- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: error handling
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
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
Discussion Boards
Community
Resources
Forums
Blogs
- 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
01-20-2004 04:36 PM
01-20-2004 04:36 PM
error handling
i would like to know any error handling mechanism in unix scripting i.e when i used the cat command if there as no file,then it throws an error.is there any methood to catch that error,if so please let me know.
THANKING YOU,
HARI KRISHNA
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2004 04:51 PM
01-20-2004 04:51 PM
Re: error handling
like
if [ -f /
then
cat
else
echo file not present
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2004 05:06 PM
01-20-2004 05:06 PM
Re: error handling
When a function or a command terminates, it sets a flag indicating the status
of the termination. In other words, if the function or command was successful
in executing, it returns a value indicating its success. The values (or error
codes) normally used are listed in the exit section. These values are only
conventions; shell scripts normally use these conventions, but programs in
general do not.
When you execute a shell command incorrectly, you usually get an error
message. What usually happens is the shell command returns an error code.
If the error code is, say, 2, you will receive a message indicating a syntax error
has occurred.
You can return error codes from your shell programs and functions in two
ways. The exit statement can return any value you specify by using the
following format:
exit n
where n can be an integer from 0 to 255. You can return error codes from
functions by using the return statement:
return n
To check the return value of the last command you executed, you can use a
parameter called $?.
sks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2004 05:47 PM
01-20-2004 05:47 PM
Re: error handling
You could also define a function that checks $? and is called after every command with possible error-exit codes. This is the way the SG package control scripts use, for instance. It would look something like this:
function test_exitcode {
ec=$?
if $ec
then
echo "Command returned non-null exitcode. This is not right. Exiting."
exit
fi
}
In your case, your script would start with the function definition and then:
cat
test_exitcode
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2004 07:42 PM
01-20-2004 07:42 PM
Re: error handling
You can also use 'trap ERR' or '||'. See man sh-posix.
#!/usr/bin/sh
trap "echo no file found" ERR
cat file 2>&-
or
#!/usr/bin/sh
cat file 2>&1 || echo no file found
Regards.