So if you ever decide to get into shell scripting, it’s a wonderful world. Until you run into snags that you once thought you had the hang of. Let’s take shell conditionals, for example.
So i’m trying to test the output of grep and do something if it returns a result. After setting it up the usual way:
if [ -n `ls | grep something' ]; then
echo "Something Exists";
fi
But by doing it that way, I kept getting the error:
./scnotify.sh: line 3: [: 2841.c: binary operator expected
which for the life of me I couldn't figure out. Eventually I figured it out though...and all that the error was, was instead of a single '[' and ']‘ wrapping the comparison, it requires a double…i.e.
if [[ -n `ls | grep something` ]]; then
echo "Success";
fi
Hope this saves someone some headache in the future.
beacuse execute “ls | grep something” maybe more than one parameter generate,
It truly works, I had a problem with this line
if [ grep -i mars $1 ] ; then
so I changed it for
if [[ -n `grep -i mars $1` ]] ; then
do you know why it works?
or you do it right.
if [ -n "`ls | grep something'" ]; then
echo “Something Exists”;
fi
As a matter of fact, this did save me from a headache. Thanks!
thanks! that really helped me =) no headache here, thanks to you
You just saved my sanity. Thanks for this post. VERY helpful!
big THX 4 saving me be4 headache ;-)
it helped! but why?!? doesn’t matter ;)
GreetX from Switzerland
VoiceX
Totally saved me a headache. Thanks!
Great, saved me a headache. After researching, the bash man page says “Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.
Doesn't seem to work that way though.
[ -d ${TMPDIR} ] || mkdir ${TMPDIR} # <– Works
[[ -e ${FILE} ]] || echo "$FILE not found" # <– Doesn't work without double brackets
Heachache resolved…thx for the tip!
Thanks for the advice, little tricky bug to figure out.
Thank you. Fixed error when I used wildcard between braces
Thank you.
Thanks!
Thank you very much. This is a bug I had in a cygwin bashrc script and as I am not used to bash syntax this tricky problem eluded me till now. I have found the if condition that was causing me the problem.