[Tfug] Problem checking return code in bash script

Nate nate at torzo.com
Wed Oct 7 10:04:06 MST 2009


Oops, I just realized I was getting a return from su, which is different
than what you're doing.  So I replaced 'boguscommand' with a call to a
script which actually does return non-zero, but again, it works fine.

#!/bin/sh
sudo su -l -c '/tmp/fail.sh' db2inst1
if [ ! $? = 0 ]; then echo "failed" && exit; fi
echo "got here"

$ sh test.sh
I'm going to fail right now
failed
$

You could put in an "echo $?" right after the command to see if your
command really and truly is returning non-zero, and it's not something
else, possibly with command precedence or something.

Nate

On 10/07/2009 09:56 AM, Nate wrote:
> I just created a test script that is identical to yours except that I
> call a different script since I don't have ruby or rails, and it works
> perfectly.
> 
> #!/bin/sh
> sudo su -l -c 'boguscommand' user2
> if [ ! $? = 0 ]; then echo "failed" && exit; fi
> echo "got here"
> 
> $ sh test.sh
> -su: boguscommand: not found
> failed
> $
> 
> So no clue on why it doesn't work for you...
> 
> Nate
> 
> On 10/05/2009 09:23 PM, Chad Woolley wrote:
>> I'm writing a script which automates system setup, with logic like this:
>>
>> sudo su -l -c "ruby ~/railsci/setup_rails_dependencies.rb" ci
>> if [ ! $? = 0 ]; then echo "Unable to setup rails dependencies" && exit; fi
>>
>> However, even if the command returns a non-zero return code (tested by
>> running manually), the script keeps going and doesn't exit.
>>
>> What am I doing wrong?  References/links to docs are welcome.
>>
>> Thanks,
>> -- Chad
>>
>> _______________________________________________
>> Tucson Free Unix Group - tfug at tfug.org
>> Subscription Options:
>> http://www.tfug.org/mailman/listinfo/tfug_tfug.org
> 
> _______________________________________________
> Tucson Free Unix Group - tfug at tfug.org
> Subscription Options:
> http://www.tfug.org/mailman/listinfo/tfug_tfug.org




More information about the tfug mailing list