Skip to content
Snippets Groups Projects
Commit 8b2c38ec authored by Florent Gluck's avatar Florent Gluck
Browse files

Added correction serie2

parent e361f998
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env bash
# Notes:
# - en utilisant la récursivité
# - introduce stat command
# - do not compute size of folders
# - maange empty directories
# - hidden files should be ignored
# Get the size of a link given as parameter
function getsize {
stat "$1" | grep Size | cut -c9- | cut -d' ' -f1
}
# a folder is given as the unique parameter
# finds the biggest file in this folder
function findbig {
BIGSIZE=0
# If folder is empty skip it
CONTENT=$(ls "$1")
if [ -d "$1" -a -n "$CONTENT" ]; then
# For each file in the folder
for f in "$1"/*; do
# If directory find the biggest file of this directory
if [ -d "$f" ]; then
NEWSIZE=0
NEWFILE=$(findbig "$f")
if [ -n "$NEWFILE" ]; then
NEWSIZE=$(getsize "$NEWFILE")
fi
# If file find the size of this file
else
NEWSIZE=$(getsize "$f")
NEWFILE="$f"
fi
# Update biggest size and file
if [ $NEWSIZE -gt $BIGSIZE ]; then
BIGFILE="$NEWFILE"
BIGSIZE=$NEWSIZE
fi
done
echo "$BIGFILE" # return the name of the biggest file
fi
}
# Print usage if number / type of argument is incorrect
if [ $# -ne 1 -o ! -d $1 ];then
echo "Number of argument is incorrect or parameter is not a folder" >&2
echo "Usage:" >&2
echo -e "\t$(basename $0) folder" >&2
exit 1
fi
# Find the biggest file and recomptue its size for display
BIGFILE=$(findbig "$1")
if [ -n "$BIGFILE" ]; then
BIGSIZE=$(getsize $BIGFILE)
echo -e "$BIGFILE: $BIGSIZE"
fi
\ No newline at end of file
#!/usr/bin/env bash
# A few comments:
# - carefull that users can have the same group name than user name
# - what happens if program is used without an argument
# - what happens if program is used with a non existing username
# Returns the GID of the username given in the first parameter
function getusergid {
grep "^$1:" /etc/passwd | cut -d':' -f4
}
# Returns the groupname of the corresponding GID given in first parameter
function getgroupbygid {
grep "^.*:x:$1" /etc/group | cut -d':' -f1
}
# Retuns the main group of a username given in parameter
function getusergroup {
GID=$(getusergid $1)
if [ -n "$GID" ]; then
getgroupbygid $GID
fi
}
# Return all additional group names for the username given in the first parameter
function getusergroups {
grep -E "(:|,)$1(,|\$|\s)" /etc/group | cut -d':' -f1
}
USER=$1
if [ -z "$USER" ]; then
echo "Usage:" >&2
echo -e "\t$(basename $0) username" >&2
exit 1
fi
MGROUP=$(getusergroup $USER)
AGROUP=$(getusergroups $USER)
if [ -n "$MGROUP" -o -n "$AGROUP" ]; then
for G in $MGROUP $AGROUP; do
echo -n "$G "
done
echo ""
fi
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment