Unix Shell - Non-zero exit code breaks bash loop

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2005 > Non-zero exit code breaks bash loop





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Non-zero exit code breaks bash loop
drodrig

2005-04-19, 8:01 am

Hi. I have a simple script that untars some tarfiles. In the script is
the following loop:

for i in *.tar
do
tar -xf $i
done

The problem is that if the tar program exits with a non-zero exit code,
the loop breaks. Does anyone know how to prevent the loop from
breaking?

Thanks.

David Rodrigues

Michael Tosch

2005-04-19, 6:06 pm

drodrig wrote:
> Hi. I have a simple script that untars some tarfiles. In the script is
> the following loop:
>
> for i in *.tar
> do
> tar -xf $i
> done
>
> The problem is that if the tar program exits with a non-zero exit code,
> the loop breaks. Does anyone know how to prevent the loop from
> breaking?
>
> Thanks.
>
> David Rodrigues
>


You certainly have used "set -e" somewhere, or use #!/bin/bash -e,
or your bash is compiled with a -e default.
This means: exit on non-zero exit status.

You switch it off with

set +e
for ...

--
Michael Tosch @ hp : com
drodrig

2005-04-19, 6:06 pm

Thank you. The "-e" must be compiled in. The "set +e" worked perfectly.

Moshe Jacobson

2005-04-21, 5:59 pm

drodrig <drodrig@magicbrain.com> wrote:
> for i in *.tar
> do
> tar -xf $i
> done


> The problem is that if the tar program exits with a non-zero exit code,
> the loop breaks.


I suspect you're not showing us the entire script. Normally, that
would not cause the loop to break. Do you have maybe a "set -e"
somewhere else in your code?

Moshe

--
*** SPAM BLOCK: Remove bra before replying! ***
http://runslinux.net :: moshe at runslinux dot net :: AIM: Jehsom
Moshe Jacobson

2005-04-23, 2:50 am

drodrig <drodrig@magicbrain.com> wrote:
> for i in *.tar
> do
> tar -xf $i
> done


> The problem is that if the tar program exits with a non-zero exit code,
> the loop breaks.


I suspect you're not showing us the entire script. Normally, that
would not cause the loop to break. Do you have maybe a "set -e"
somewhere else in your code?

Moshe

--
*** SPAM BLOCK: Remove bra before replying! ***
http://runslinux.net :: moshe at runslinux dot net :: AIM: Jehsom
drodrig

2005-04-27, 7:56 am

Actually, that isn't the whole script, but I tested the snippet above
in a separate file before sending.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com