Bash getopts ,参数未传递给选项

Bash getopts , argument not being passed to option

我有以下脚本 它应该像这样工作:

bash code.sh -u user -g

它应该 return 输入用户的组,但它 return 是执行脚本的用户的组。

    #!/bin/bash

while getopts u:fgd options; do
    case $options in
    u)
    user = $OPTARG 2>/dev/null;
    if grep -q $OPTARG /etc/passwd == 1 2>/dev/null ; 
    then
      echo "user exists";

    else
      echo "user doesnt exist"
    fi 
    ;;
    g) if $user;
    then    
        echo $user;
        echo "the groups of the user are: ";
        groups $user ;
    else
        echo "user missing"
    fi 
    ;;
    f) echo "output f" ;;

    d) echo "output d" ;;


    esac

done

基本上在 g) 中,如果有一个输入的用户,该用户不会被捕获为参数,这应该可以工作

这是您的代码的注释版本:

#!/bin/bash

while getopts u:fgd options; do
    case $options in
    u)
    user = $OPTARG 2>/dev/null;    # no spaces around the assignment, it is not set
                                   # even if it worked, you still have a problem
                                   # user is always set, even if the user does not
                                   # exist but you want to test if the variable is
                                   # set later, you should move this into the if
    if grep -q $OPTARG /etc/passwd == 1 2>/dev/null ; # your test does not work,
                                                      # if does test the return value
                                                      # of the command directly, not
                                                      # with a '== 1'
                                                      # also this grep pattern will
                                                      # match more than users
# also, a return code of 1 (or any non-zero value) means failure

    then
      echo "user exists";

    else
      echo "user doesnt exist"
    fi 
    ;;
    g) if $user;   # here you want to check if user is set, but bash
                   # will try to run the content of user and test the return
                   # value. Since user was not set above, it will be empty and 
                   # return no error. After that, the command groups will be called
                   # with an empty value and return your own groups.
    then    
        echo $user;
        echo "the groups of the user are: ";
        groups $user ;
    else
        echo "user missing"
    fi 
    ;;
    f) echo "output f" ;;

    d) echo "output d" ;;


    esac

done

下面一个做你想让它做的事:

#!/bin/bash
set -euo pipefail    # this is what can be called bash strict mode
                     # it will make bash exit with an error if any
                     # command return non-zero, even in pipe or if
                     # a variable is undefined

user="" # define the variable as empty

while getopts u:fgd options; do
    case "$options" in
        u)
            if grep -q "^$OPTARG:" /etc/passwd ; then # note the '^' to make sure the
                                                     # string starts at the beginning
                                                     # of the line
                user="$OPTARG"              # store the value, otherwise remain empty
                echo "user exists"
            else
                echo "user doesnt exist"
            fi
        ;;
        g)
            if [ -n "$user" ] ; then          # check if the string is non-zero length
                echo "$user"
                echo "the groups of the user are: "
                groups "$user" 
            else
                echo "user missing"
            fi 
        ;;
        f) echo "output f" ;;

        d) echo "output d" ;;
        *) echo "error, invalid option $options" ;; # handle the default case
    esac
done

我还强烈建议通过 bash -x 查看您的代码,它会按原样打印出每个命令 运行。