0

How would you create an if statement that contains an or operator to evaluate the first parameter passed in?

I've copied examples straight from the links at the bottom with no luck. I've also copied other examples I've found online unsuccessfully.

# not working
if [ "$1" = "restart" || "$1" = "reload"]; then
      echo "you passed in $1"
      exit 3 
fi

# not working
if [[ "$1" = "restart" || "$1" = "reload"]]; then
      echo "you passed in $1"
      exit 3 

# not working
if [ $1 = "restart" || $1 = "reload"]; then
      echo "you passed in $1"
      exit 3
fi

# not working
if [ $1 == "restart" || $1 == "reload"]; then
      echo "you passed in $1"
      exit 3
fi

# not working
if [ $1 == "restart" || $1 == "reload"]; then
      echo "you passed in $1"
      exit 3
fi

# not working
if [ "$1" = "restart" || "$1" = "reload" ]; then
      echo "you passed in $1"
      exit 3 
fi

# not working
if [ "$1" == "restart"]  || [ "$1" == "reload" ]; then
      echo "you passed in $1"
      exit 3 
fi

plus every other syntax I can find....

I am given one of the following errors

/etc/init.d/gitlab: 192: [: =: unexpected operator

or

/etc/init.d/gitlab: 192: [: missing ]

or

/etc/init.d/gitlab: 194: [: missing ]
/etc/init.d/gitlab: 194: [: ==: unexpected operator

Resources:
http://tldp.org/LDP/abs/html/comparison-ops.html

https://unix.stackexchange.com/questions/47584/in-a-bash-script-using-the-conditional-or-in-an-if-statement

How to do a logical OR operation in Shell Scripting

http://www.thegeekstuff.com/2010/06/bash-if-statement-examples/

4

2 回答 2

4

Usually, you'd do it like this:

if [ "$1" = restart -o "$1" = reload ]; then

The reason your last example doesn't work is simply because test uses = for equality testing, not ==. If you write it like this instead, it works:

if [ "$1" = restart ] || [ "$1" = reload ]; then

For the record, the reason you're getting the error [: missing ] is because the shell snatches the || you've written, and considers it the end of the command, so the [ command in question only gets the arguments up until that point, not finding any ending ].

Also, you must make sure to keep a space between the last argument and the ], since the terminating ] needs to be an argument of its own and you need, therefore, the shell to split it properly.

As a minor aside, you don't need to quote the restart and reload strings. Since they contain no spaces or expansions, quoting them is a noop.

On the other hand, this also works:

[[ "$1" == "restart" || "$1" == "reload" ]]

But that's because the [[ command is a completely separate (though similar) command to [ and uses a completely different syntax (and is actually a shell built-in, which is why the shell knows not to snatch the || from it).

In Bash, see help test and help [[ for more details, including the -o operator.

于 2013-10-06T01:27:48.213 回答
0

This one should work for you, but if not you case is another alternative.

if [ "$1" = "restart" ] || [ "$1" = "reload" ]; then
      echo "you passed in $1"
      exit 3 
fi

For case you would use:

case "$1" in
    'start')
        echo "Starting application"
        /usr/bin/start
        ;;
    'stop')
        echo "Stopping application"
        /usr/bin/stop
        ;;
    'restart')
        echo "Usage: $0 [start|stop]"
        ;;
esac

Syntax for the case example was borrowed from http://www.thegeekstuff.com/2010/07/bash-case-statement/

于 2013-10-06T01:30:52.753 回答