检查KSH脚本参数的数量

Jam*_*sev 1 ksh

myscript使用以下参数执行时,它将失败,错误为255(如下)

         1     2     3                4       5     6     7
myscript value value /my/path/to/file my_file /tmp/ value value
Run Code Online (Sandbox Code Playgroud)

检查传递的参数数量

if [ ${#} -ne 7 ]
echo ${#}          // Actually prints 7
then
    echo "ERROR 255: Must provide the following 7 parameters:
                   one two three four five six seven"
    exit 255
fi
Run Code Online (Sandbox Code Playgroud)

所以...如果数字不是7,退出,但要告诉数字是什么.. 7.

世界变得疯了吗?:)

she*_*ter 5

你确定你的问题中没有拼写错误吗?

echo ${#}          // Actually prints 7
if [ ${#} -ne 7 ]
then
    echo "ERROR 255: Must provide the following 7 parameters:
                   one two three four five six seven"
    exit 255
fi
Run Code Online (Sandbox Code Playgroud)

具有echo ${#}之间的if [ ... ]then是一个语法错误,并让我KSH炸毁;-)否则,我认为你的代码看起来是正确的.

但为什么不使用更新的ksh数学评估功能(也许这将解决您的问题).

echo ${#}          // Actually prints 7
if (( ${#} != 7 )) ; then
    echo "ERROR 255: Must provide the following 7 parameters:
                   one two three four five six seven"
    exit 255
fi
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.