检查 KSH 脚本参数的数量

发布于 2024-12-22 04:35:00 字数 507 浏览 1 评论 0原文

当使用以下参数执行 myscript 时,它会失败并显示错误 255(如下)

         1     2     3                4       5     6     7
myscript value value /my/path/to/file my_file /tmp/ value value

检查传递参数的数量

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

所以...如果数字不是 7,则退出,但请告诉数字是多少... 7.

世界疯了吗? :)

When myscript is executed with the following arguments it fails with error 255 (below)

         1     2     3                4       5     6     7
myscript value value /my/path/to/file my_file /tmp/ value value

Checking number of passed arguments

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

So ... If the number is not 7, exit, but do tell what the number is .. 7.

Has the world gone mad? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疑心病 2024-12-29 04:35:00

你确定你的问题没有错别字吗?

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

if [ ... ]then 之间添加 echo ${#} 是一个语法错误,会让我的 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

我希望这有帮助。

Are you sure you don't have a typo in your question?

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

Having the echo ${#} in between the if [ ... ] and the then is a syntax error, and makes my ksh blow up ;-) Otherwise I think your code looks correct.

But why not use newer ksh math evaluation features (maybe this will fix your problem).

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

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文