bash 中的参数检查未正确测试

Parameter checks in bash not testing correctly

我已经为此绞尽脑汁了一段时间...我试图让我的代码做出如下反应:

If no parameters, go to menu
If more OR less than 4 parameters, call error and go to menu
If exactly 4 parameters, write to file and exit

我无法以任何方式让它工作,如果你能提供帮助,我将不胜感激!

username=
firstname=
surname=
password=

    if test "$#" = 0; then
    {
    menu 
    }
    elif test "$#" = 4; then
    {
   echo Error
    sleep 2
    menu
    }
    else {
     echo Done
     echo "$firstname" "$surname" >> "$username".log
    echo "$password" >> "$username".log
    curdate=$(date +'%d/%m/%Y %H:%M:%S')
    echo "$curdate" >> "$username".log
    sleep 2
    clear
    exit
    }
    fi

在 bash 中,数字比较不是用 = 完成的,而是用 -eq 及其同类完成的。 (= 用于字符串比较。)

所以你想要这样的东西。我将用更常见的 [ 符号替换您的 test

if [ "$#" -eq 0 ] ; then
{
    menu 
}
elif [ "$#" -eq 4 ] ; then
...

莫特