简体   繁体   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

it works but leaves an error: 它有效,但会出现错误:

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

BTW, Why the inner parenthesises needs to be escaped? 顺便说一句,为什么要删除内括号?

Bash uses spaces to separate tokens, which are then passed as arguments to the commands (in this case the command is test ). Bash使用空格分隔令牌,然后将令牌作为令牌传递给命令(在这种情况下,命令为test )。 See the man page for test for more details. 有关更多详细信息,请参见手册页以进行test

To solve, you need to add spaces between the operands. 要解决此问题,您需要在操作数之间添加空格。

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

If you are using bash and don't mind giving up POSIX compatibility, the following is a little simpler: 如果您使用的是bash并且不介意放弃POSIX兼容性,则以下内容会更简单一些:

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

You can use || 您可以使用|| instead of -o , and the parentheses don't need to be escaped. 而不是-o ,并且不需要对括号进行转义。

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

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