LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-30-2007, 06:35 PM   #1
Mr_Safe
LQ Newbie
 
Registered: Jun 2007
Posts: 0

Rep: Reputation: 0
"[: too many arguments" Error In An If Statement


I'm working on a small script and I get the "[: too many arguments" error in this small section of code. I've been looking at it for a while but I don't see anything wrong. This is my first bash script so some eyes with more experience than mine would be appreciated! This is the offending if statement.

<code>
if [ -z $(grep "BUILDSUCCESS" mantislog) ]; then
echo 'The component failed to build!'
echo $d >> ../faillist
else
echo 'Changes were succsesful!'
fi
</code>
 
Old 07-01-2007, 11:31 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Use:

Code:
grep -q "BUILDSUCCESS" mantislog&&echo "Changes were succsesful!"\
||{ echo "The component failed to build!";echo "$d" >> ../faillist;}
Or, if you prefer the if .. then syntax:

Code:
if ! grep -q "BUILDSUCCESS" mantislog;then
	echo "The component failed to build!"
	echo "$d" >> ../faillist
else
	echo "Changes were succsesful!"
fi
 
Old 07-01-2007, 01:24 PM   #3
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
The problem is the shell's stupid word splitting rules, the argument to -z must be a single $IFS delimited word but the $(...) is split into multiple words (which it shouldn't be, this is broken but unfortunately, this idiocy is actually specified by POSIX, so all shells (except zsh) do this).

Thus,

Code:
if [ -z "$(grep BUILDSUCCESS mantislog)" ]; then
should also suffice.
 
Old 07-02-2007, 11:10 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

I don't use the [[ notation often, but it appears to be in the posix standard (so it's portable), and it appears to not require quotes (so it solves the immediate problem):
Code:
#!/bin/sh

# @(#) s1       Demonstrate double square bracket.

set -o nounset
set -o posix
echo " sh version: $BASH_VERSION" >&2
set -o | grep -i posix

s="hello, world"
echo
echo " String is :$s:"

echo " Expect error:"
if [ -z $s ]
then
  echo zero length
else
  echo " non-zero-length or error, status = $?"
fi

echo
echo " Expecting no error:"
if [[ -z $s ]]
then
  echo zero length
else
  echo " non-zero-length or error, status = $?"
fi

exit 0
producing:
Code:
% ./s1
 sh version: 2.05b.0(1)-release
posix           on

 String is :hello, world:
 Expect error:
./s1: line 15: [: hello,: binary operator expected
 non-zero-length or error, status = 2

 Expecting no error:
 non-zero-length or error, status = 1
Apparently the exit status also is different ... cheers, makyo
 
Old 07-02-2007, 08:46 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Actually, when I did my (ksh) course at Sequent some yrs ago, they pointed out that [[ ]] is better than [] and why, but I can't remember exactly why now.
There are explanations on the web though.
I thnk it's something to do with handling empty/null vars properly.
Personally, I've always used [[ ]] since then and it works for me.
 
  


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
error: Error for wireless request "Set Mode" (8B06) : invalid argument "roaming" penguin chick Linux - Wireless Networking 5 08-22-2008 01:16 PM
LXer: Position Statement to the Dunc-Tanc "experiment" LXer Syndicated Linux News 0 10-27-2006 01:03 AM
What is wrong in the statement: $line || echo "error" && break; nadavvin Linux - General 1 08-05-2006 05:25 AM
IPW2200 Make error macro "create_workqueue" passed 2 arguments, but takes just 1 jplev22 Linux - Wireless Networking 2 09-16-2005 11:34 AM
What does "Syntax error in parameters or arguments" mean with Evolution mr_matti Linux - Software 3 10-26-2004 02:19 PM


All times are GMT -5. The time now is 05:03 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration