- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: error in script
Operating System - Linux
1752795
Members
5891
Online
108789
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2005 03:12 AM
09-07-2005 03:12 AM
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.
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.
- Tags:
- exit status
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2005 03:35 AM
09-07-2005 03:35 AM
Solution
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.
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...
UNIX because I majored in cryptology...
- « Previous
-
- 1
- 2
- Next »
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP