bash 为什么 'if [ $# > 0 ] 有问题;然后'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5219546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why is wrong with 'if [ $# > 0 ] ; then'?
提问by schemacs
why does it still produce output in the third command?
为什么它仍然在第三个命令中产生输出?
$cat sh.sh
#!/bin/sh
echo $#
if [ $# > 0 ] ; then
base=`basename .c`
echo $base
fi
$ sh sh.sh a.c
1
a
$ sh sh.sh
0
.c
I use this file:/usr/share/doc/opencv-doc/examples/c/build_all.sh to build the c examples for opencv packages,but failed with similar errors.
我使用此文件:/usr/share/doc/opencv-doc/examples/c/build_all.sh 为 opencv 包构建 c 示例,但因类似错误而失败。
回答by codaddict
You need to use -gtin place of >.
您需要使用-gt代替>.
You can use >if you are using double parenthesis construct.
>如果您使用双括号构造,则可以使用。
回答by pepoluan
Remember that [isa command. /usr/bin/[, to be exact. It is linked to the command /usr/bin/test
记住这[是一个命令。/usr/bin/[,准确地说。它链接到命令/usr/bin/test
So, [ $# > 0is identical to test $# > 0, i.e., redirect test $#'s output to the 0file.
因此,[ $# > 0与test $# > 0,即,将test $#的输出重定向到0文件相同。
If you're using bash... now that's a different story altogether :-)
如果您正在使用bash...现在完全不同了:-)
回答by dogbane
You should use -gt(Greater than) in your condition, not >. Look at man testfor more information.
您应该-gt在您的情况下使用(Greater than),而不是>. 查看man test更多信息。
if [ $# -gt 0 ] ; then
base=`basename .c`
echo $base
fi

