简体   繁体   中英

bash error “command not found” when using if with “or” operator

So I am trying out this if elif code in UNIX using "bash script" and it is giving me this Output

script[11]: pc4: command not found
script[14]: pc4: command not found

instead of "-o" I previously had "|" as the "or" Operator but that wasn't working. Also, I had brackets before but yeah...

#!/bin/bash
host=`hostname`
test1="pc4"
test2="pc5"
prod1="t01"
prod2="t02"
prod3="t03"
prod4="t04"
path="/home/test1/test2/test3"

if "$host" = "$test1" -o "$host" = "$test2"
   then
       echo "test loaded"
elif "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4"
   then
       echo "prod loaded"
else
       echo "bad host"
fi

edit: fixed it, it had to be:

if [ "$host" = "$test1" -o "$host" = "$test2" ];
...
...
elif [ "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4" ];

thanks to all!

You aren't calling a commmand; -o is an operator of the test command, although its use is not recommended.

if test "$host" = "$test1" -o "$host" = "$test2"
then
   echo "test loaded"
elif test "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4"
then
   echo "prod loaded"
else
   echo "bad host"
fi

Instead, use multiple calls to test joined by the shell operator || . (Line breaks may be easily added after || for readability.)

if test "$host" = "$test1" || 
   test "$host" = "$test2"
then
    echo "test loaded"
elif test "$host" = "$prod1" ||
     test "$host" = "$prod2" ||
     test "$host" = "$prod3" || 
     test "$host" = "$prod4"
then
    echo "prod loaded"
else
    echo "bad host"
fi

Finally, you may want to consider using a case statement instead:

case $host of
    "$test1"|"$test2" ) echo "test loaded" ;;
    "$prod1"|"$prod2"|"$prod3"|"$prod4") echo "prod loaded" ;;
    *) echo "bad host" ;;
esac

Also try:

#!/bin/bash
host=`hostname`
test1="pc4"
test2="pc5"
prod1="t01"
prod2="t02"
prod3="t03"
prod4="t04"
path="/home/test1/test2/test3"

if [[ "$host" == "$test1" ]] || [[ "$host" == "$test2" ]]
then
  echo "test loaded"
elif [[ "$host" == "$prod1" ]] || [[ "$host" == "$prod2" ]] || [[ "$host" == "$prod3" ]] || [[ "$host" == "$prod4" ]]
then
  echo "prod loaded"
else
  echo "bad host"
fi

Though it is true that only an "=" is needed for string equality comparison, the "=" is also used as the assignment operator. Hence, so as to reduce possible confusion and as a 'best practice', I would use "==" for string equality . It so happens that "==" is just a synonym for "=" but I prefer not to use the same operator for multiple purposes.
NOTE: I am not implying POSIX compatibilty.

You need to put your if and elif conditions in brackets:

#!/bin/bash
host=`hostname`
test1="pc4"
test2="pc5"
prod1="t01"
prod2="t02"
prod3="t03"
prod4="t04"
path="/home/test1/test2/test3"

if [ "$host" = "$test1" -o "$host" = "$test2" ]
   then
       echo "test loaded"
elif [ "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4" ]
   then
       echo "prod loaded"
else
       echo "bad host"
fi

Please note that the space before and after [ is crucial.

-o is a syntax supported by test command or [ command.

So enclose your condition withing [ and ] as shown:

if [ "$host" = "$test1" -o "$host" = "$test2" ];
then
       echo "test loaded"
elif [ "$host" = "$prod1" -o "$host" = "$prod2" -o "$host" = "$prod3" -o "$host" = "$prod4" ];
then
       echo "prod loaded"
else
       echo "bad host"
fi

Refer this link

try this;

#!/bin/bash
host=`hostname`
test1="pc4"
test2="pc5"
prod1="t01"
prod2="t02"
prod3="t03"
prod4="t04"
path="/home/test1/test2/test3"

if [ "$host" = "$test1" ] || [ "$host" = "$test2" ] ; then
echo "test loaded"
elif [ "$host" = "$prod1" ] || [ "$host" = "$prod2" ] || [ "$host" = "$prod3" ] || [ "$host" = "$prod4" ] 
   then
       echo "prod loaded"
else
       echo "bad host"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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