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/

Community
  • 1
  • 1
spuder
  • 17,437
  • 19
  • 87
  • 153

2 Answers2

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.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • I find `if [ "$1" = restart -o "$1" = reload ]; then` is false because $1 is null. I think there is something bigger going on – spuder Oct 06 '13 at 01:52
  • In case you're using `$1` inside a function, it is worth noting that it, in the scope of that function, refers to the first argument *to the function*, rather than to the script as a whole. – Dolda2000 Oct 06 '13 at 01:54
  • you are right, thank you. Is there any work around to get what parameters the script was called using ($1) from inside a function? – spuder Oct 06 '13 at 02:02
  • You could either pass the parameter(s) explicitly when calling the function; or you could assign it to another named variable, which would then be available inside the function. – Dolda2000 Oct 06 '13 at 02:20
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/

Jon
  • 1,398
  • 9
  • 14
  • I thought that would work, but it gives me /etc/init.d/gitlab: 192: [: unexpected operator – spuder Oct 06 '13 at 01:38
  • I believe `[` is a part of busybox so it may not be available. I've noticed many android devices using `case` instead of it for what I assume is a similar reason. – Jon Oct 06 '13 at 01:43