Bash Scripting if statement [duplicate] - bash

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 8 years ago.
I don't know why this isn't working. Please help!
#!/bin/bash
clear
echo "Enter an option"
read $option
if ("$option" == 1) then
echo "Blah"
fi
I tried like this
if ("$option" -eq 1) then
I can't see why the if statement isn't being run. All I want to do is check what the user entered and do something depending on the value entered.

The syntax for an equality check is:
if [[ $option == 1 ]]; then
echo "Blah"
fi
Or, for compatibility with older non-bash shells:
if [ "$option" = 1 ]; then
echo "Blah"
fi
In either one, the whitespace is important. Do not delete the spaces around the square brackets.

That is not the syntax for an if statement in bash. Try this:
if [ "$option" = "1" ]; then
echo "Blah"
fi

I'm not sure where you got your syntax from...try this:
if [ "$option" -eq 1 ]; then
In the shell, [ is a command, not a syntactic construct.
Alternatively, you can use an arithmetic context in bash:
if (( option == 1 )); then

Bash if uses [ or [[ as test constructs, instead of parenthesis which are used in other languages.

Related

test the return value of function with parameter in bash/shell scripting without accessing $? or creating new variable [duplicate]

This question already has answers here:
What is the proper way to test a Bash function's return value?
(5 answers)
Closed 2 years ago.
I would like to do the following in shell scripting:
if [ (is_number $arg1) -ne 0 ] || [ (is_number $arg2) -ne 0 ] ; then
printf "Exit. Valid input must be positive integer.\n"
return 1
fi
But it gave me syntax error. Do you have better solution? Thanks!
I want a solution without accessing $? or storing return value into variable, unless such answer doesn't exist.
I see you guys taking advantage of 0 and not 0. It make sense since 0 signify whether succeed or not.
Now, speaking it in a pure syntax way, I want to do the following:
if [ (is_number $arg1) -ne int ] || [ (is_number $arg2) -ne int] ; then
printf "Exit. Valid input must be positive integer.\n"
return 1
fi
Please provide a way, thanks.
Do you have better solution?
Call the actual functions.
is_number() { [[ "$1" =~ ^[0-9]+$ ]]; }
if ! is_number "$arg1" || ! is_number "$arg2"; then

Shell always evaluates both factors of &&? [duplicate]

This question already has answers here:
Why does 'test -n' return 'true' in bash?
(2 answers)
Closed 2 years ago.
When $thing is null this quits...
thing=`command_that_could_return_null`
echo "thing is" $thing
if [ -z $thing ]; then exit; fi
...but this...
if [ -n $thing ] && [ $thing = "special_value" ]; then
do_the_special_thing
fi
comes out with
[: =: unexpected operator
I don't understad.
When $thing is null the first factor of the && evaluates to false and therefore the second should not be evaluated, but it appears that it is being evaluated?
I guess it's just a feature of sh that I have to write this as two ifs?
If you fix your quote problem (which #CharlesDuffy mentioned) - your bottom 3 lines work just fine:
if [ -n "$thing" ] && [ "$thing" = "special_value" ]; then
do_the_special_thing
fi
Also, you referred to variables being "null", but the relevant term is a variable that is "unset". In Bourne shell terminology, "null" refers to the empty string.

Shell script: Why can't the if statement make a logical comparison?

I'm new to Unix and Linux in general and failed to make a logical comparison within an if statement.
I'm sure this is a very basic mistake but I just can't find the error.
if (7+3 == 10); then
echo "nice this works"
elif (7+3 == 73); then
echo "too bad string concatenation"
else
echo "I really don't understand shell"
fi
Echo: I really don't understand shell.
I would expect you to see this error message twice: 7+3: command not found -- did you?
Single sets of parentheses run the enclosed commands in a subshell, so you're attempting to execute the command 7+3 with two arguments, == and 10 (or 73)
Arithmetic evaluation occurs within double parentheses
if ((7+3 == 10)); then
echo "nice this works"
elif ((7+3 == 73)); then
echo "to bad string concatenation"
else
echo "I really don't understand shell"
fi
nice this works
See http://mywiki.wooledge.org/ArithmeticExpression
The operator your are using == is used for string comparisons. The right way to do it would be with the -eq (equal) operator:
if [ 10 -eq 10 ]; then
echo "10 = 10"
fi
and you also need to use the right parenthesis for the if [ ] (you can look this up in the bash with 'man test')
The correct syntax would be
if [ $((7+3)) -eq 10 ]; then
echo "nice this works"

Use string as bash variable name in alternative value expansion [duplicate]

This question already has answers here:
How can I look up a variable by name with #!/bin/sh (POSIX sh)?
(4 answers)
Closed 7 years ago.
How can I use the value of one variable as the name of another variable in an alternative value expansion (${var+alt}) in bash?
I would think that
#!/bin/bash
cat='dog'
varname='cat'
if [ -z ${`echo "${varname}"`+x} ]; then
echo 'is null'
fi
should be roughly equivalent to
#!/bin/bash
if [ -z ${dog+x} ]; then
echo 'is null'
fi
but when I try to do this, I get
${`echo "${cat}"`+x}: bad substitution
I guess part of the problem is that the subshell doing the command substitution doesn't know about $varname anymore? Do I need to export that variable?
My reason for doing this is that I learned from this answer how to check if a variable is null, and I'm trying to encapsulate that check in a function called is_null, like this:
function is_null {
if [ $# != 1 ]; then
echo "Error: is_null takes one argument"
exit
fi
# note: ${1+x} will be null if $1 is null, but "x" if $1 is not null
if [ -z ${`echo "${1}"`+x} ]; then
return 0
else
return 1
fi
}
if is_null 'some_flag'; then
echo 'Missing some_flag'
echo $usage
exit
fi
I'm not sure if I understand your problem.
If I got, what you need is eval command.
$ cat='dog'
$ varname='cat'
$ echo ${varname}
cat
$ eval echo \$${varname}
dog

Comparing strings for equality in ksh

i am testing with the shell script below:
#!/bin/ksh -x
instance=`echo $1 | cut -d= -f2`
if [ $instance == "ALL" ]
then
echo "strings matched \n"
fi
It's giving this error in the if condition:
: ==: unknown test operator
is == really not the correct syntax to use?
I am running on the command line as below
test_lsn_2 INSTANCE=ALL
Could anybody please suggest a solution.
Thanks.
To compare strings you need a single =, not a double. And you should put it in double quotes in case the string is empty:
if [ "$instance" = "ALL" ]
then
echo "strings matched \n"
fi
I see that you are using ksh, but you added bash as a tag, do you accept a bash-related answer?
Using bash you can do it in these ways:
if [[ "$instance" == "ALL" ]]
if [ "$instance" = "ALL" ]
if [[ "$instance" -eq "ALL" ]]
See here for more on that.
Try
if [ "$instance" = "ALL" ]; then
There were several mistakes:
You need double quotes around the variable to protect against the (unlikely) case that it's empty. In this case, the shell would see if [ = "ALL" ]; then which isn't valid.
Equals in the shell uses a single = (there is no way to assign a value in an if in the shell).
totest=$1
case "$totest" in
"ALL" ) echo "ok" ;;
* ) echo "not ok" ;;
esac
I'va already answered a similar question. Basically the operator you need is = (not ==) and the syntax breaks if your variable is empty (i.e. it becomes if [ = ALL]). Have a look at the other answer for details.

Resources