Bash 如何在UNIX的getopts while循环中使用case命令

Bash 如何在UNIX的getopts while循环中使用case命令,bash,unix,Bash,Unix,我目前有一个Unix程序,在该程序中,我试图正确使用一个getopts while循环,其中嵌套了一个case命令。其中getopts while循环是一个case命令,case中有两个选项,它们都是until循环。以下是当前代码: while getopts ufn: user do case "$user" in u) tty=$(who | grep "$user " | cut -d\ -f 2)

我目前有一个Unix程序,在该程序中,我试图正确使用一个getopts while循环,其中嵌套了一个case命令。其中getopts while循环是一个case命令,case中有两个选项,它们都是until循环。以下是当前代码:

while getopts ufn: user
do

    case "$user"

    in

            u)

                    tty=$(who | grep "$user " | cut -d\  -f 2)
                    until who | grep "$user "  > /dev/null
                    do
                            sleep 60
                    done
                            echo "$user has logged onto $tty";;
            f)

                    until find | home/students/shaunkolkman/$user
                    do
                            test -d $user || test -f $user
                            sleep 20
                    done
                            echo "$user is a file or directory";;

    esac

done

user=$1
选项u是一个直到循环,用于查找要登录的用户并显示该用户的tty。选项f是查找文件或目录的直到循环。直到循环工作得非常好。在我解决这个问题后,将添加选项n

下面是一个snipppet示例,其中描述了正在发生的事情

./mon4 -f vex

我面临的问题是-f中的f被视为变量。以及-f,它们仍然作为一个选项从getopts读取。我不明白为什么vex不被解读为变量

如前所述,需要值的选项需要冒号

while getopts uf:n: user
#               ^
#              /
#    notice ---

选项字符串需要在f后面加一个冒号,表示该选项接受一个值。例如:虽然getopts u:f:n:…遗憾的是,这些注释和答案不适用于我的代码。把冒号放在正确的位置并没有改变问题。要么我没有看到正确的语法,要么是其他问题。