PDA

View Full Version : Help: Simple Bash if/then/else error.



ewanchic
February 27th, 2008, 06:04 PM
I'm going to assume this really should be simple for someone. Simple, I know I must be doing something wrong.

only relevant info:


discPtr=$(cdrskin dev=$discDrive -msinfo 2>/dev/null)
echo "Disc Ptr:" + $discPtr

if ["$discPtr" = ""]

then
mkisofs -v -o $discBurn/track.iso -R $backupLoc

else
mkisofs -v -o $discBurn/track.iso -R -C $discPtr -M $discDrive $backupLoc

fi


It does go through both conditions...and then all of a sudden it's like i have a syntax error somewhere:



+ echo 'Disc Ptr:' + 326400,489728
Disc Ptr: + 326400,489728
+ '[326400,489728' = ']'
./burn.zimbra: line 16: [326400,489728: command not found
+ mkisofs -v -o /tmp/discBurn/track.iso -R -C 326400,489728 -M /dev/scd0 /media/sdc1/raws/backups/zimbra


I've studied other code, it for the most part, the program executes correctly. What am I doing wrong to produce the funny string comparison?

The Whole Script, in case anyone is interested:


#!/bin/bash -x

echo "Initializing Burn.Zimbra"
discDrive=/dev/scd0
discBurn=/tmp/discBurn
discPtr=$(cdrskin dev=$discDrive -msinfo 2>/dev/null)
backupLoc=/media/sdc1/raws/backups/zimbra

echo "Disc Ptr:" + $discPtr

rm -R $discBurn
mkdir -p $discBurn

wait
echo "Creating Track"
if ["$discPtr" = ""]

then
mkisofs -v -o $discBurn/track.iso -R $backupLoc

else
mkisofs -v -o $discBurn/track.iso -R -C $discPtr -M $discDrive $backupLoc

fi

echo "Burning Disc"
cdrskin dev=$discDrive -v padsize=300k -multi -tao -eject $discBurn/track.iso

rm -R $discBurn
echo "Backup Complete"



Thanks for the help :)

lloyd_b
February 27th, 2008, 07:59 PM
For the "if" condition, try

if test -z "$discPtr"

"test -z ..." returns TRUE if the associated string is zero length.

Lloyd B.

ewanchic
February 27th, 2008, 08:39 PM
Still Strange :confused:



....
+ discPtr=326400,489728
+ backupLoc=/media/sdc1/raws/backups/zimbra
+ echo 'Disc Ptr:' + 326400,489728
Disc Ptr: + 326400,489728
+ rm -R /tmp/discBurn
rm: cannot remove `/tmp/discBurn': No such file or directory
+ mkdir -p /tmp/discBurn
+ wait
+ echo 'Creating Track'
Creating Track
+ t -z 326400,489728
./burn.zimbra: line 16: t: command not found
+ mkisofs -v -o /tmp/discBurn/track.iso -R -C 326400,489728 -M /dev/scd0 /media/sdc1/raws/backups/zimbra
....

WW
February 27th, 2008, 08:52 PM
Try putting spaces before and after the square brackets.

ewanchic
February 27th, 2008, 09:19 PM
Code 1:


if [ "$discPtr" = '' ]


Result 1:


+ '[' 326400,489728 = '' ']'
+ mkisofs -v -o /tmp/discBurn/track.iso -R -C 326400,489728 -M /dev/scd0 /media/sdc1/raws/backups/zimbra

Is this correct output?!
-----

Code 2:


if ( "$discPtr" = '' )


Result 2:


+ 326400,489728 = ''
./burn.zimbra: line 16: 326400,489728: command not found

-----

Code 3:


if [ "$discPtr" = "" ]


Result 3:


+ 326400,489728 = ''
./burn.zimbra: line 16: 326400,489728: command not found

---------

Code 4:


if [ test -z "$discPtr" ]


Result 4:


+ '[' test -z 326400,489728 ']'
./burn.zimbra: line 16: [: -z: binary operator expected

--------

Mr. C.
February 27th, 2008, 09:46 PM
Code 1 is missing a final double quote.
Code 2 executes the evaluation of the variable in trailing stuff in a subshell
Code 3 should not output what you indicate - something is wrong with the test...
Code 4 a left bracket [ and test are the same thing - use only the left bracket with its matching right bracket, or only use test:


if [ -z "$discPtr" ]
if test -z "$discPtr"



$ discPtr='326400,489728'
if [ "$discPtr" = "" ] ; then echo empty; else echo not empty; fi
not empty
$ if [ -z "$discPtr" ] ; then echo empty; else echo not empty; fi
not empty
$ discPtr=
$ if [ -z "$discPtr" ] ; then echo empty; else echo not empty; fi
empty

MrC

ewanchic
February 27th, 2008, 10:57 PM
Code 1 is missing a final double quote.
That wasn't a double quote, that was two single quotes, sorry.


Code 2 executes the evaluation of the variable in trailing stuff in a subshell
Can you explain this one a little more to me. Thanks (^_^)


Code 3 should not output what you indicate - something is wrong with the test...
That is why I'm so confused. I'm 99.99% sure this should be working. In most cases it does. It's like the bash doesn't want to work anymore?!, but I am assuming it really me that created the problem.


Code 4 a left bracket [ and test are the same thing - use only the left bracket with its matching right bracket, or only use test:


if [ -z "$discPtr" ]
if test -z "$discPtr"
Thanks for the Correction (^_^)






$ discPtr='326400,489728'
if [ "$discPtr" = "" ] ; then echo empty; else echo not empty; fi
not empty
$ if [ -z "$discPtr" ] ; then echo empty; else echo not empty; fi
not empty
$ discPtr=
$ if [ -z "$discPtr" ] ; then echo empty; else echo not empty; fi
empty

MrC

Confirmed. I received the same results from commandline as you did. I aslo did:

discPtr=$(cdrskin dev=/dev/scd0 -msinfo 2>/dev/null)

from command line, same good,correct results

New Program:


#!/bin/bash -x

discDrive=/dev/scd0
discPtr=$(cdrskin dev=$discDrive -msinfo 2>/dev/null)

if [ "$discPtr" = "" ]; then echo empty; else echo not empty; fi
if [ -z "$discPtr" ]; then echo empty; else echo not empty; fi


discPtr='326400,489782'
if [ "$discPtr" = "" ]; then echo empty; else echo not empty; fi
if [ -z "$discPtr" ]; then echo empty; else echo not empty; fi


discPtr=
if [ "$discPtr" = "" ]; then echo empty; else echo not empty; fi
if [ -z "$discPtr" ]; then echo empty; else echo not empty; fi


Results:


+ discDrive=/dev/scd0
++ cdrskin dev=/dev/scd0 -msinfo
+ discPtr=0,163216
+ '[' 0,163216 = '' ']'
+ echo not empty
not empty
+ '[' -z 0,163216 ']'
+ echo not empty
not empty
+ discPtr=326400,489782
+ '[' 326400,489782 = '' ']'
+ echo not empty
not empty
+ '[' -z 326400,489782 ']'
+ echo not empty
not empty
+ discPtr=
+ '[' '' = '' ']'
+ echo empty
empty
+ '[' -z '' ']'
+ echo empty
empty


So it works on commandline, but not in a bash script?! Or is it really working?!

Mr. C.
February 28th, 2008, 12:09 AM
That wasn't a double quote, that was two single quotes, sorry.

Wow, in my browser window, two single quotes and one double quote are indistinguishable. This is the first time I've been tripped up by this. Time to change my mono-spaced font.

Anyway,


Can you explain this one a little more to me. Thanks (^_^)

When a command is finished executing, it returns a status. The if ( some_command ); ... form of if tests that status.

Here, a command is run in a subshell, and its exit status what the if test evaluates:

$ if ( ls /nonexistent ) ; then echo Command succeeded: exit status $? ; else echo Command failed: exit status $?; fi
ls: /nonexistent: No such file or directory
Command failed: exit status 1
$ if ( ls / > /dev/null ) ; then echo Command succeeded: exit status $? ; else echo Command failed: exit status $?; fi
Command succeeded: exit status 0

What you had earlier looked essentially like this:


if ( "$somevar" = '' )

so the some_command is the interpolated value of $somevar as arg0, = as arg1 and the empty string as arg2. This is all executed in a subshell. Since the variable's value was "326400,489728", the command executed would be:


326400,489728 =

hence you get a :


326400,489728: command not found



So it works on commandline, but not in a bash script?! Or is it really working?!
Of course it works, and will work the same either way. If it is failing, you have an error somewhere. At this point, I'm not sure what your script looks like, and what the exact output is. If this doesn't clarify the problems, please post the script and output without modification.

MrC