如何检查在基于 busybox 的 Unix 系统中是否没有传递命令行参数?

How can I check if there are no command line arguments passed in a busybox based Unix system?

我正在尝试创建一个 if else 语句,其中 if 检查是否存在命令行参数,else 用于检查是否存在 none。我已经用 if else 和两个 ifs 试过了。每当我 运行 没有任何参数时,我都会收到错误消息:

sh: 0: 未知操作数.

如果我 运行 使用命令行参数,它工作正常。 如果我 运行 带有 if 的脚本我得到错误:

sh: 缺少 ]

这是我最近试过的两个案例,

if [  -gt 0 ];
then
    ch=
    stri=
    thefunction $ch $stri
fi

# User input not working

if [ -z "" ];
then
    stat="Y"
    while [ $stat == "[Yy]" ];
    do
        printf "\nPlease enter a choice. Enter 1 for manual.\n"
        read ch
        flag=1
        if [ $ch -eq 3];
        then
            printf "\nPlease enter string to be searched.\n"
            read stri
        fi
        thefunction $ch $stri $flag
        printf "Do you wanna continue? \n Press 'Y' to continue..."
        read stat
    done
fi

if [  -gt 0 ];
then
    ch=
    stri=
    thefunction $ch $stri
else
    stat="Y"
    while [ $stat == "[Yy]" ];
    do
        printf "\nPlease enter a choice. Enter 1 for manual.\n"
        read ch
        flag=1
        if [ $ch -eq 3];
        then
            printf "\nPlease enter string to be searched.\n"
            read stri
        fi
        thefunction $ch $stri $flag
        printf "Do you wanna continue? \n Press 'Y' to continue..."
        read stat
    done
fi

我也尝试过使用多个 [[ ]] 代替单个 [ ]、if ! 以及我在 Google 上可以找到的大部分内容。

How can I check if there are no command line arguments passed in a busybox based Unix system?

您检查参数的计数是否等于零。

if [ $# -eq 0 ]; then
   echo "No arguments passed"
fi

如果没有参数,</code> 将展开为空:</p> <pre><code>if [ -gt 0 ];

扩展为:

if [ -gt 0 ];

由于 -gt 0 ][ 的无效参数,工具会报错。

请注意 bash 是 space 知道的,而 3]; 可能意味着 3 ];

同时 [ $stat == "[Yy]" ] 检查 stat 是否等于字符串 "[Yy]"。而 ==[ 命令的扩展,请改用 =。要检查 $stat 是否等于 Yy,请使用 [ "$stat" = "y" -o "$stat" = "Y" ] 或使用 case 构造。