"[: 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> |
Use:
Code:
grep -q "BUILDSUCCESS" mantislog&&echo "Changes were succsesful!"\ Code:
if ! grep -q "BUILDSUCCESS" mantislog;then |
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 |
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 Code:
% ./s1 |
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. |