Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to deal with an exit code 1

Status
Not open for further replies.

Baraka69

Programmer
Apr 22, 2003
32
DE
I have an AWK script that is being called within a shell script.

The shell script takes a directory as argument and runs the AWK script in that directory (and all subdirectiories) on a specific file (always the same file name).

This file somtimes has lines that are longer than 3,000 bytes and then my AWK script exits with an error message.

I can get rid of the lines that are too long with an extra SED script, but that script is quite slow, because it needs to unzip the original file, make a file containing only the offending lines, another file without the offending lines, backup the original file and finally zip all 3 files again.

I want my script to run normally until the AWK scripts exits with the error and then (and only then) run the SED script and afterwards re-run the AWK script.

Since I am new to shell scripts I have no clue how to get the return/exit code of a script.

Any help is gretly appreciated.



-----SHELL SCRIPT: (relevant parts) HP-UX-11.00
cd $1
find . -name INPUTFILE.gz | while read filename
do
gzcat $filename | awk -f AWKFILE
done
cd -
-----
-----SED SCRIPT:
cd $1
gunzip INPUTFILE.gz
sed -n '/PATTERN/p' INPUTFILE >> OFFENDINGFILE
sed '/PATTERN/d' INPUTFILE >> GOODFILE
mv INPUTFILE BACKUPFILE
mv GOODFILE INPUTFILE
gzip -9 INPUTFILE BACKUPFILE OFFENDINGFILE
cd -
-----
 
Hi,
try add after 'done' (before 'cd -') lines:
awkcode=$?
if [ $awkcode -ne 0 ]; then
# awk ended which error
else
# no error
fi

by the way. You can suppress output of error message. instead done write done 2> /dev/null

regards Boris
 
First of all, thanks for the help Boris. Now new questions come up:
How about testing for success within the success loop?

e.g.
-----SHELL SCRIPT
cd $1
find . -name INPUTFILE.gz | while read filename; do
gzcat $filename | awk -f AWKFILE
awkExitCode=$?
if [ awkExitCode -ne 0 ]; then
# step 1 - rename file
mv INPUTFILE.gz INPUTFILE_original.gz
# step 2 - make file with pattern only
gzcat INPUTFILE_original.gz | sed -n '/PATTERN/p' | gzip > INPUTFILE_PATTERN_only.gz
# step 3 - make file without pattern
gzcat INPUTFILE_original.gz | sed '/PATTERN/d' | gzip > INPUTFILE.gz
# step 4 - run the original awk script again
gzcat INPUTFILE.gz | awk -f AWKFILE
done
cd -
-----

Of course, I want to continue to step 2 only, if step 1 was successful. Especially step 4 should not be run (and overwrite the original file) if step 1 failed.

Question 1
How do I get successive success established? Can I use $? again and again?

Question 2
Is "awkexitcode=$?; if [ awkexitcode -ne 0 ]; then" the same as "if [ $? -ne 0 }; then"?

Question 3
Boris (aka Boria) suggested that you can suppress output of error mesages by substituting "done" with "done 2> /dev/null". Does this work for suppressing the output of the error message of my awk script? I assume it only suppresses the output of the shell script, or more precise the output of the do-done loop.
 
Hi,
Q 1. after each shell command $? means error code of this command.
Q 2. Yes.
Q 3. In your case
while [....]; do
.....
done 2> /dev/null
all error message from all command inside loop 'while' will be suppressed.

Regards Boris
 
Thanks again Boris, now I'm set and will hopefully be able to finish my script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top