0

I have a case script and need to input username in option -u user... how to type variable user to be able in this option list groups of specific typed user, thanks for help

#!/bin/bash
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
            -h | --help)
                    echo "usage=$(basename "$0")[-h][-g][-u user][-e]
                    -h      [show help]
                    -g      [show only primary group, otherwise all]
                    -u user [show groups of specific user, otherwise logged] user
                    exit
                    ;;
            -g | --primary)
                    shift;
                    echo "under work"
                    ;;
            -u $user | --user)
                    shift;
                    echo "$(groups user)"
                    ;;
esac; shift; done
if [[ "$1" == '--' ]];then
    shift;
fi

1 Answers1

0

Since shift discards the "current" first argument ($1) and shifts all succeeding arguments "one to the left", all you need to do is modify your -u case as follows:

        -u | --user)
            shift;
            echo "$(groups $1)"
        ;;

assuming that the user is specified right after -u or --user (like in -u my_user).

Notice also that at the moment you should remove the shift statement in your second case, marked "under work", as it will discard the next argument on the command-line thanks to the "global" shift instruction after your case block.

AdminBee
  • 22,803