1754403 Members
2976 Online
108813 Solutions
New Discussion юеВ

Re: exit in script!

 
SOLVED
Go to solution
mick001
Advisor

exit in script!

I have a question about the exit in the script.
The idea is that the script stop when the tar command fails. so that the files are not removed. did I do it correct?

Thanks


#!/bin/bash

set -v
cd /cdr1/pmc
#
filename=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.tar
indexfile=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.txt

ls -l /cdr1/pmc >$indexfile

mkdir -p /cdr1/backup-tijd

for file in $(ls *.BF); do
mv ${file} /cdr1/backup-tijd
done

cd /cdr1/backup-tijd

tar cvf $filename .
if [ $? -ne 0 ];
then
echo "tar failed"
exit
else
rm /cdr1/backup-tijd/*
rmdir /cdr1/backup-tijd/
fi


gzip $filename
if [ $? -ne 0 ];
then
echo "gzip-failed"
exit
else
touch gzip-ok
fi
exit
11 REPLIES 11
Pete Randall
Outstanding Contributor

Re: exit in script!

Looks OK to me. You could also test it by inserting a simple command like "ll" in place of the tar.


Pete

Pete
Piergiacomo Perini
Trusted Contributor

Re: exit in script!

Hi,

seems good to me !

regards
pg
Vibhor Kumar Agarwal
Esteemed Contributor

Re: exit in script!

You can also give you exit codes.

Like exit 3, exit 4, etc.
This will help you trace later on, as to from where has the code exited.
Vibhor Kumar Agarwal
James R. Ferguson
Acclaimed Contributor

Re: exit in script!

Hi:

Your handling of the return status from 'tar' and 'gzip' are fine.

I'll offer one unrelated suggestion.

The remove of the contents of the directory and then the directory itself can be coded with a recursive remove to handle both. More importantly, the use of the "*" wildcard which the shell will expand into a list of all of the files in the directory, could lead to an "argument list too long" error. [Of course, I'll grant that that was more a problem on older OS versions than newer ones where much larger argument space can be configured; but nevertheless, there is better form].

Thus, to remove your directory and its contents, simply do:

# cd /
# rm -r rm /cdr1/backup-tijd

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: exit in script!

Hi:

Oops!!! That shoud read:

# cd /
# rm -r /cdr1/backup-tijd

Regards!

...JRF...

A. Clay Stephenson
Acclaimed Contributor

Re: exit in script!

The most significant change I would suggest is to always save ${?} to another variable so that subsequent operations do not alter it. This becomes especially useful when printing error messages. Good practice requires that error messages generally be sent to stderr rather than stdout and that you exit with a non-zero status on failures and zero if okay.

I would make changes similar to this:

typeset -i STAT=0
gzip ${filename}
STAT=${?} # save status in another variable
if [[ ${STAT} -ne 0 ]]
then
echo "gzip-failed; status ${STAT}." >&2
exit ${STAT}
else
touch gzip-ok
fi
exit ${STAT}
If it ain't broke, I can fix that.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: exit in script!

Regarding deleting recursively, just a small enhancement:

rm -rf /directory

This will help if the file is read only.
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: exit in script!

Use this script.

#!/bin/bash
# Exit Status
# 0 - Success; 1 - Tar failed;2 - gunzip failed
set -x
cd /cdr1/pmc

filename=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.tar
indexfile=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.txt

ls -l /cdr1/pmc >$indexfile

mkdir -p /cdr1/backup-tijd

for file in $(ls *.BF); do
mv -f ${file} /cdr1/backup-tijd
done

cd /cdr1/backup-tijd

tar cvf $filename .
if [ $? -ne 0 ];
then
echo "tar failed"
exit 1
else
rm -rf /cdr1/backup-tijd/
fi

gzip $filename
if [ $? -ne 0 ];
then
echo "gzip-failed"
exit 2
else
echo "gunzip-ok"
fi

exit 0
set +x

hth.
Easy to suggest when don't know about the problem!
mick001
Advisor

Re: exit in script!

Hi

Why do you use set -x ?

Thanks