繁体   English   中英

为什么shell脚本给我太多参数错误?

[英]Why the shell script gives me a too many argument error?

#! /bin/bash

if [ !\(-f new.bash -o -d new.bash\) ]
then
    echo "Neither"
else
    echo "yes"
fi

它有效,但会出现错误:

/file_exist_or_not.bash      
./file_exist_or_not.bash: line 3: [: too many arguments
yes

顺便说一句,为什么要删除内括号?

Bash使用空格分隔令牌,然后将令牌作为令牌传递给命令(在这种情况下,命令为test )。 有关更多详细信息,请参见手册页以进行test

要解决此问题,您需要在操作数之间添加空格。

if [ ! \( -f new.bash -o -d new.bash \) ]

如果您使用的是bash并且不介意放弃POSIX兼容性,则以下内容会更简单一些:

if [[ ! ( -f new.bash || -d new.bash ) ]]; then

您可以使用|| 而不是-o ,并且不需要对括号进行转义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM