LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-19-2008, 09:16 AM   #1
webaccounts
Member
 
Registered: May 2008
Location: Columbus Ohio
Distribution: CentOS 5.9/6.4 x64
Posts: 44

Rep: Reputation: 19
Limit in bash for conditions?


I'm getting this error when I run a script.
Code:
[root@lnxapp bin]# ./update-chapter-webpage
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
./update-chapter-webpage: line 270: [: too many arguments
This is the code. "if" is line 270 that the error refers too.
Code:
state=`cat $webroot/$basename/state`
        if [ $state != "Alabama" ] && [ $state != "Alaska" ] && [ $state != "Arizona" ] && [ $state != "Arkansas" ] && [ $state != "California" ] && [ $state != "Colorado" ] && [ $state != "Connecticut" ] && [ $state != "Delaware" ] && [ $state != "Florida" ] && [ $state != "Georgia" ] && [ $state != "Hawaii" ] && [ $state != "Idaho" ] && [ $state != "Illinois" ] && [ $state != "Indiana" ] && [ $state != "Iowa" ] && [ $state != "Kansas" ] && [ $state != "Kentucky" ] && [ $state != "Louisiana" ] && [ $state != "Maine" ] && [ $state != "Maryland" ] && [ $state != "Massachusetts" ] && [ $state != "Michigan" ] && [ $state != "Minnesota" ] && [ $state != "Mississippi" ] && [ $state != "Missouri" ] && [ $state != "Montana" ] && [ $state != "Nebraska" ] && [ $state != "Nevada" ] && [ $state != "New Hampshire" ] && [ $state != "New Jersey" ] && [ $state != "New Mexico" ] && [ $state != "New York" ] && [ $state != "North Carolina" ] && [ $state != "North Dakota" ] && [ $state != "Ohio" ] && [ $state != "Oklahoma" ] && [ $state != "Oregon" ] && [ $state != "Pennsylvania" ] && [ $state != "Rhode Island" ] && [ $state != "South Carolina" ] && [ $state != "South Dakota" ] && [ $state != "Tennessee" ] && [ $state != "Texas" ] && [ $state != "Utah" ] && [ $state != "Vermont" ] && [ $state != "Virginia" ] && [ $state != "Washington" ] && [ $state != "Washington DC" ] && [ $state != "West Virginia" ] && [ $state != "Wisconsin" ] && [ $state != "Wyoming" ] && [ $state != "Canada" ] && [ $state != "Europe" ] && [ $state != "Other" ]
        then
                echo "Unknown" > $webroot/$basename/state
        fi
My question is, I don't see whats wrong with that area other than maybe it doesn't like that many conditions. Is there something I'm missing?

Last edited by webaccounts; 09-19-2008 at 09:17 AM.
 
Old 09-19-2008, 09:31 AM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
I was able to recreate the same error when $state held more than one word. So it fails when
Code:
echo $state
returns something like this:
Code:
word1 word2..etc
Make sure there is only one word inside $state and that there are no spaces.
 
Old 09-19-2008, 09:47 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep: Reputation: 75
Hi.

There are a few ways of dealing with this:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate command test as bracket, quoting.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset

echo
echo " Results with single [, no quotes:"
for state in Minnesota "New Jersey"
do
  if [ $state != Minnesota ]
  then
    echo " state is not Minnesota: :$state:"
  else
    echo " state is Minnesota"
  fi
done

echo
echo " Results with single [, quotes:"
for state in Minnesota "New Jersey"
do
  if [ "$state" != Minnesota ]
  then
    echo " state is not Minnesota: :$state:"
  else
    echo " state is Minnesota"
  fi
done

echo
echo " Results with double [, no quotes:"
for state in Minnesota "New Jersey"
do
  if [[ $state != Minnesota ]]
  then
    echo " state is not Minnesota: :$state:"
  else
    echo " state is Minnesota"
  fi
done

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Results with single [, no quotes:
 state is Minnesota
./s1: line 14: [: too many arguments
 state is Minnesota

 Results with single [, quotes:
 state is Minnesota
 state is not Minnesota: :New Jersey:

 Results with double [, no quotes:
 state is Minnesota
 state is not Minnesota: :New Jersey:
Best wishes ... cheers, makyo

Last edited by makyo; 09-19-2008 at 09:49 AM.
 
Old 09-19-2008, 10:47 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
Quote correctly your variables unless you are SURE that they will never hold special characters like spaces.
 
Old 09-19-2008, 10:51 AM   #5
webaccounts
Member
 
Registered: May 2008
Location: Columbus Ohio
Distribution: CentOS 5.9/6.4 x64
Posts: 44

Original Poster
Rep: Reputation: 19
Thanks for the help. I didn't realize the 2 word ones.

I fixed it by changing it from
Code:
if [ $state != "Alabama" ] .....
to
Code:
if [ "$state" != "Alabama" ] .....
 
Old 09-19-2008, 11:12 AM   #6
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by webaccounts View Post
I fixed it by changing it from
Code:
if [ $state != "Alabama" ] .....
to
Code:
if [ "$state" != "Alabama" ] .....
You probably want to use
Code:
if [ "x$state" != "xAlabama" ] .....
This handles the problem that $state has not been initialized.
 
Old 09-21-2008, 03:01 AM   #7
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 136Reputation: 136
Quote:
Originally Posted by David1357 View Post
You probably want to use
Code:
if [ "x$state" != "xAlabama" ] .....
This handles the problem that $state has not been initialized.
I found that following code is more readable:
case "x$state" in
xAlabama | \
xAlaska | \
xArizona | \
xLastValue)
DoSomething
;;

*)
DoSomethingElse
;;
esac
 
Old 09-21-2008, 08:25 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep: Reputation: 75
Hi.

The uninitialized variable situation could be addressed generally with bash setting nounset. The double brackets also handle uninitialized variables -- for example:
Code:
#!/bin/bash -

# @(#) s2       Demonstrate comparison of single and double brackets.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)

echo
echo " Results, single:"
if [ $state != Minnesota ]
then
  echo " State is not Minnesota ( single [] ), :$state:"
fi

echo
echo " Results, double:"
if [[ $state != Minnesota ]]
then
  echo " State is not Minnesota ( double [[]] ), :$state:"
fi

exit 0
Producing:
Code:
% ./s2

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Results, single:
./s2: line 11: [: !=: unary operator expected

 Results, double:
 State is not Minnesota ( double [[]] ), ::
I like the case suggestion.

We don't know how often this code is executed, but if it is a "large number" of times, then there is a clever post at http://groups.google.com/group/comp....be3dcd9?hl=en# that may be useful. It shows how a hash function can be written for bash. The displayed code used a complicated method for the hash, which I replaced that with cksum because I found that easier to read. The array needs to be initialized, but after that, the hash of the text to lookup only needs to be calculated (and presumably checked for existence). If I had to guess, it's probably more useful for a large number of items through which to search, as opposed to a large number of executions, and also where a dynamic capability is required. Even if not useful in this situation, it might be for some other problem. It would be interesting to compare the methods -- an exercise for the reader.

Best wishes ... cheers, makyo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script "if then" with multiple conditions dohpaz Programming 8 05-24-2012 04:31 PM
-bash: ulimit: open files: cannot modify limit: Operation not permitted pjglinuxq Linux - Server 3 06-11-2010 06:19 AM
functions in case conditions viveksnv Programming 4 04-02-2008 10:15 PM
Multiple If conditions keysorsoze Programming 4 12-12-2007 01:07 AM
Bash while loop with 2 conditions. elinenbe Programming 2 10-14-2007 12:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration