diff --git a/Cours/03-Scripting/exercices/correction/convimg b/Cours/03-Scripting/exercices/correction/serie1/convimg similarity index 100% rename from Cours/03-Scripting/exercices/correction/convimg rename to Cours/03-Scripting/exercices/correction/serie1/convimg diff --git a/Cours/03-Scripting/exercices/correction/sigproc b/Cours/03-Scripting/exercices/correction/serie1/sigproc similarity index 100% rename from Cours/03-Scripting/exercices/correction/sigproc rename to Cours/03-Scripting/exercices/correction/serie1/sigproc diff --git a/Cours/03-Scripting/exercices/correction/serie2/getbig b/Cours/03-Scripting/exercices/correction/serie2/getbig new file mode 100755 index 0000000000000000000000000000000000000000..698504855fd0095d3759a97650bd5ac85c2019d9 --- /dev/null +++ b/Cours/03-Scripting/exercices/correction/serie2/getbig @@ -0,0 +1,62 @@ +#!/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 diff --git a/Cours/03-Scripting/exercices/correction/serie2/getgroups b/Cours/03-Scripting/exercices/correction/serie2/getgroups new file mode 100755 index 0000000000000000000000000000000000000000..9aa41ae0046c83c3a0a8b68d91b6b8174e12a6aa --- /dev/null +++ b/Cours/03-Scripting/exercices/correction/serie2/getgroups @@ -0,0 +1,47 @@ +#!/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