Quick BASH Help

Status
Not open for further replies.

Blastomatic

Wise, Aged Ars Veteran
119
Can someone tell me why this isn't evaluating?

Code:
# Check if action is valid and run function if it is.
if [ $1 == "start" || "stop" || "restart" ]; then
	$1
else 
	echo "$1 is not a valid action, please use start, stop, or restart."
fi

I'm using the same sort of input verification earlier in the script and that's working fine, am I missing something stupid?
 

tmedwards

Seniorius Lurkius
29
Can someone tell me why this isn't evaluating?

Code:
# Check if action is valid and run function if it is.
if [ $1 == "start" || "stop" || "restart" ]; then
Because that's not valid test command syntax—in sh/bash/ksh/etc '[' is traditionally an alias for the test command, whether it's the binutil or a built-in. In test, the OR operator is '-o'.

Also. A double equal sign ('==') is not a standard test operator, string equality is simply a single equal sign ('=').

The correct test syntax would be:
Code:
if [ $1 = "start" -o $1 = "stop" -o $1 = "restart" ]; then

That said. Chaining multiple test invocations together with the shell's OR operator ('||') also works, as you found out.
 

Blastomatic

Wise, Aged Ars Veteran
119
Can someone tell me why this isn't evaluating?

Code:
# Check if action is valid and run function if it is.
if [ $1 == "start" || "stop" || "restart" ]; then
Because that's not valid test command syntax—in sh/bash/ksh/etc '[' is traditionally an alias for the test command, whether it's the binutil or a built-in. In test, the OR operator is '-o'.

Also. A double equal sign ('==') is not a standard test operator, string equality is simply a single equal sign ('=').

The correct test syntax would be:
Code:
if [ $1 = "start" -o $1 = "stop" -o $1 = "restart" ]; then

That said. Chaining multiple test invocations together with the shell's OR operator ('||') also works, as you found out.
This is a lot more elegant, thanks for the advice.
 
Status
Not open for further replies.