1752795 Members
5891 Online
108789 Solutions
New Discussion

Re: error in script

 
SOLVED
Go to solution
Sameer_Nirmal
Honored Contributor

Re: error in script

You are trying to have a exit status in each
of those loops ( tar and gzip). In the first tar loop, if the tar failed, you are supposed to have exit 1 and not exit 0 . Then put exit 0 after echo "tar ok" . If successful return of any status should be exit 0 becuase shell understand it that way.
If you are taking care of exit status return at each loop, you don't need to have exit at the end. Those last exit will be always the return values for any condition of any loop.
So you will get always 0 or 1 as return values irrespective of the actual return from the loops.

Mel Burslan
Honored Contributor
Solution

Re: error in script

Mick,

exit 0 is not an error code let alone being the only one. Au contraire, exit 0 is the exit code for error-free completion of an executable as a convention but you can call successful completion as exit code 5 and trap for this one if your heart desires so. It is not an absolute requirement as long as you know what you are doing.

In my opinion, your script could be better of with this type of struct:

#!/bin/bash
cd /home/test

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

ls -l /home/test >$indexfile

mkdir -p /home/backup-tijd

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

cd /home/backup-tijd
tar cvf $filename .

if [ $? -ne 0 ];
then echo "tar failed"
exit 1
else
echo "tar ok"
fi


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

echo "all procedure completed successfully"
exit 0

so, according to the above, if you test the exit code of the script, you can tell the user where exactly the failure was.


________________________________
UNIX because I majored in cryptology...