From e97ddd8be0f2d6e29dd3587d5ad1ae1a86d46f0b Mon Sep 17 00:00:00 2001 From: Patrick Marchand Date: Fri, 9 Nov 2018 17:33:44 -0500 Subject: [PATCH] Simplification of list_account_by_UID in ftpadmin Setting / unsetting IFS variables can be removed by setting it only in the loop context. The for cat can be replaced by a simpler while read loop. Proper variable quoting was added. Changed the way modif was optionally passed, this removes the extraneous ':' at the end. echo(1) the lines as we go instead of building an array, this removes the possibility of sub-shell screwups. --- scripts/ftpadmin.sh | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/scripts/ftpadmin.sh b/scripts/ftpadmin.sh index 6973875..6b09bf6 100755 --- a/scripts/ftpadmin.sh +++ b/scripts/ftpadmin.sh @@ -60,36 +60,24 @@ get_user_login_by_UID() { list_accounts_by_UID() { uid=$1 - account_list='' - oldIFS=IFS - IFS=$'\n' - - for line in `cat $VPASSWD_PATH` + while IFS=$'\n' read -r line; do - line_uid=`echo $line | cut -d":" -f3` + line_uid="$(echo "$line" | cut -d":" -f3)" - if [ ! "$uid" ] || [ "$line_uid" == "$uid" ]; then - username=`get_user_login_by_UID $line_uid` - account=`echo $line | cut -d":" -f1` - path=`echo $line | cut -d":" -f6` - if [ -r $path/.size ]; then - size=`cat $path/.size` - else - size=0 - fi - #modif=`cat $path/.lastmodified` + if [[ ! "$uid" ]] || [[ "$line_uid" == "$uid" ]]; then + username="$(get_user_login_by_UID "$line_uid")" + account="$(echo "$line" | cut -d":" -f1)" + path="$(echo "$line" | cut -d":" -f6)" + size="$(du -s "$path" | cut -f 1)" + #modif="$(cat $path/.lastmodified)" # Passage en minuscule ? - #account=`echo $account | tr '[A-Z]' '[a-z]'` - #path=`echo $path | tr '[A-Z]' '[a-z]'` + #account="$(echo $account | tr '[A-Z]' '[a-z]')" + #path="$(echo $path | tr '[A-Z]' '[a-z]')" - account_list="${account_list}$username:$account:$path:$size:$modif\n" + echo "$username:$account:$path:$size${modif:+:$modif}" fi - done - - echo "$account_list" - - IFS=$oldIFS + done < "$VPASSWD_PATH" } add_account() {