User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Bash query regarding if statement...

Thu Apr 11, 2013 7:39 pm

Hi,

Probably a really silly question, but I'll ask anyways as I cannot find the answer anywhere!

Why does the following script return an error of too many arguments???

Code: Select all

read -p "Please enter your name : " name

if [ $name = "andy ibbitson" ]; then
  echo "Well done! You know my name."
else
  echo "Do you not know who Andy Ibbitson is?"
fi
I have also tried $name == "andy ibbitson" with no luck.
This is what is returned:

Code: Select all

[email protected] ~/projects/kidpi $ ./tester.sh
Please enter your name : andy ibbitson
./tester.sh: line 3: [: too many arguments
Do you not know who Andy Ibbitson is?
I am ultimately designing a bash script that my young son can use to turn on/off LEDs by just typing "green on" or "Green off" etc...

I am used to using php and this is driving me crazy. :?

Thanks for any assistance anyone can give. :D
Pi Pi Pi...

User avatar
jojopi
Posts: 3079
Joined: Tue Oct 11, 2011 8:38 pm

Re: Bash query regarding if statement...

Thu Apr 11, 2013 7:57 pm

In POSIX shells, word splitting and pathname expansion happen after variable substitution. This is practically never desirable, so you should practically always put double quotes around variable expansions.

Code: Select all

if [ "$name" = "andy ibbitson" ]; then
Without the extra double quotes here, there is a syntax error if $name is either empty or expands to more than one word.

User avatar
drewibbo
Posts: 41
Joined: Tue Feb 26, 2013 11:54 am
Location: Worthing, UK
Contact: Website

Re: Bash query regarding if statement...

Thu Apr 11, 2013 8:05 pm

jojopi wrote:In POSIX shells, word splitting and pathname expansion happen after variable substitution. This is practically never desirable, so you should practically always put double quotes around variable expansions.

Code: Select all

if [ "$name" = "andy ibbitson" ]; then
Without the extra double quotes here, there is a syntax error if $name is either empty or expands to more than one word.
Thank you so much for explaining this to me. You have helped a great deal. :D
Pi Pi Pi...

TheQuestor
Posts: 80
Joined: Wed Nov 28, 2012 9:15 am

Re: Bash query regarding if statement...

Fri Apr 12, 2013 3:21 am

jojopi wrote:In POSIX shells, word splitting and pathname expansion happen after variable substitution. This is practically never desirable, so you should practically always put double quotes around variable expansions.

Code: Select all

if [ "$name" = "andy ibbitson" ]; then
Without the extra double quotes here, there is a syntax error if $name is either empty or expands to more than one word.
I could be wrong but I thought if you are comparing 2 values then it needs to be ==
such as

Code: Select all

Platform="Raspi"

if [ "$Platform" == "Raspi" ];
then
#do something for Raspi
elif [ "$Platform" == "OSX" ];
then
#do something for OSX
else
#if neither then do something else
fi
a single = is for declaring something and a double == is for comparing

now I have used things like this in the past and it works fine for numbers or single word vars

Code: Select all

Var1=1
If [ $Var1 = 1 ] ;  #without quotes
then
#do something
else
#do something else
fi
and

Code: Select all

Var1=1
if [ "$Var1" eq "1" ];
then
#do something
fi
bash is a strange beast, and borne is even stranger

User avatar
rpdom
Posts: 15005
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Bash query regarding if statement...

Fri Apr 12, 2013 5:28 am

TheQuestor wrote:I could be wrong but I thought if you are comparing 2 values then it needs to be ==
In many languages (C, PHP, etc) that is true. But not in bash.

In fact = is used for string comparisons, even when you put a numeric value. Numeric comparisons can be done using -eq, -ne, -ge, -gt, -le and -lt as in

Code: Select all

a=1

if [ $a -eq 1 ]
then
  echo "a is 1"
fi

if [ $a -lt 2 ]
then
  echo "a is less than 2"
fi

Return to “General programming discussion”