LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "[: too many arguments" Error In An If Statement (https://www.linuxquestions.org/questions/programming-9/%5B-too-many-arguments-error-in-an-if-statement-565710/)

Mr_Safe 06-30-2007 06:35 PM

"[: 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>

radoulov 07-01-2007 11:31 AM

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


dawkcid 07-01-2007 01:24 PM

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.

makyo 07-02-2007 11:10 AM

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

chrism01 07-02-2007 08:46 PM

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. :)


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