mko
mko

Reputation: 22104

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?

Upvotes: 0

Views: 1508

Answers (2)

chepner
chepner

Reputation: 531918

If you are using bash and don't mind giving up POSIX compatibility, the following is a little simpler:

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

You can use || instead of -o, and the parentheses don't need to be escaped.

Upvotes: 2

Hans Then
Hans Then

Reputation: 11322

Bash uses spaces to separate tokens, which are then passed as arguments to the commands (in this case the command is test). See the man page for test for more details.

To solve, you need to add spaces between the operands.

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

Upvotes: 3

Related Questions