Sponsored Content
Top Forums Shell Programming and Scripting How to capture actual error message when a command fails to execute Post 302310716 by durden_tyler on Sunday 26th of April 2009 05:03:01 PM
Old 04-26-2009
Okay, let's see:

Quote:
Originally Posted by prathima
...
ls -l abc.txt 2>>errorlog.txt

In this case I understand the error message is written to the errorlog.txt
...
That's correct.

Quote:
... and I assume its bacause the return code from the command ls -l abc might return 2 if "abc" doesnt exists.
...
Nope, your assumption is incorrect.
The number 2 is not some return code that you are capturing in errlog.txt. Instead, it is the file descriptor of the stderr file. The three files - stdin, stdout and stderr are always open and have the descriptors 0, 1 and 2 assigned to them respectively. By default, stdin is your keyboard, stdout is your screen and stderr is your screen as well. Which means, by default, all error messages are directed to your screen. If you want to redirect an error message to something other than the screen (a file, for instance), then you'd use the construct "2>err.txt" after the command, which redirects the error message to the file "err.txt".

Quote:
My question is: How about if the command return non zero return code and is not equal to 2?
If the command returns a return code other than 2, it will still be captured in "err.txt" if you put the construct "2>err.txt" after that command.
An example follows:

Code:
$
$ # The telnet command returns an error code 1 if the target host is unknown
$ # The error code is stored in the "$?" variable that is echo'd right after the command.
$
$ telnet xxx
telnet: xxx: Name or service not known
xxx: Unknown host
$
$ echo $?
1
$

So now if I put "2>err.txt", then this error message should be redirected to "err.txt". As you can see, the error code here is something other than 2.

Code:
$ # try the command again and redirect the stderr to a file
$ 
$ telnet xxx 2>err.txt
$
$ # Check the file "err.txt"
$
$ cat err.txt
telnet: xxx: Name or service not known
xxx: Unknown host
$
$ # The error message was redirected to the file, and the error code was *not equal to* 2
$
$

Hope that helps,
tyler_durden
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sh Prgram to capture Log message.

Hi All, I have a log file which consists of log messages as follows -> GLOBALCALLID_CLUSTERID_B NEXT * , O(") CHARACTER JOINONBEHALFOF NEXT * , O(") CHARACTER Record 1: Rejected - Error on table IFA_MMV_CDR, column CDRRECORDTYPE.... (1 Reply)
Discussion started by: rahulrathod
1 Replies

2. Shell Programming and Scripting

How to capture status code and echo a message

Im trying to execute application and its return code is below IF Status code=o echo "........" else Staus Code =-2 DJRE then echo "......" Can any one help me how to handle the status code and echo some message. (12 Replies)
Discussion started by: laknar
12 Replies

3. Solaris

Installing gcc - recieve error message gcc : cannot execute

AIM- Install Oracle 11g on Solaris using VMWare Steps 1.Logged on as root 2.Created subfolders à /usr/local/bin & /usr/local/bin/gcc 3.Downloaded gcc & libiconv & unzipped them on my harddrive & burnt them on CD 4.Copied files from CD to /usr/local/bin/gcc 5.Terminal (root) à pkgadd -d... (8 Replies)
Discussion started by: Ackers
8 Replies

4. Shell Programming and Scripting

How to print error and exit if command fails?

Guys any tips on printing a certain error message to stderr and exiting should a command fail within a ksh script? I'm trying to null some output files. Touch isn't suitable as i need to null them. print "" > file isn't suitable as i need to check elsehere for if they are 0bytes or not. ... (5 Replies)
Discussion started by: lavascript
5 Replies

5. Shell Programming and Scripting

[Perl] Capture system call error message.

Hi, I googled a bit, but could not find the answer to my problem. But I am sure it is a common issue. I have this code: #!/bin/perl -w #-d use strict; sub remsh_test() { my $host = $_; printf "\n----\n\n"; printf "remsh to $host with system call\n"; my $result = system... (3 Replies)
Discussion started by: ejdv
3 Replies

6. Shell Programming and Scripting

Nightly job error message when trying to execute script

Hello All, I am getting the following error message when trying to execute the following script. AWK=/usr/bin/awk TR=/usr/bin/tr SED=/usr/bin/sed CAT=/usr/bin/cat MAILFILE=/home//nightly_jobs.tmp mailto=xxx@gmail.com Nigh_Status = `db2 "select TYPE from ETL.LOCK where STATUS <> 0 and... (12 Replies)
Discussion started by: NARESH1302
12 Replies

7. Shell Programming and Scripting

Capture all error message in Log file and send the Log file by email

Hi I have a requirement to write a script to capture all errors in a Logfile and send the file in email. If there is any error occurred the subject of email will be ERROR , If there are no error occurred the subject of email will be SUCCESS. So I created a Log file and put the Appropriate... (2 Replies)
Discussion started by: dgmm
2 Replies

8. Solaris

Usbcopy fails with the error message sol-11_1-live-x86.usb is not a multiple of 512

I am trying to create a live image of solaris 11.1. I have used #pkg image-update to upgrade from 11 to 11.1 already. (since only 11.1 can make images of 11.1 due to using new grub) then from within 11.1 I used pkg install install distribution-constructor to get latest usbcopy that should be... (1 Reply)
Discussion started by: taltamir
1 Replies

9. Shell Programming and Scripting

Execute Multiple Scripts and Capture Log Details

Hi All, I have a requirement to execute multiple scripts (say 4) one after the other in one script and capture log details and error messages in a log file below LOG_FILE= FILE.`date ++"%Y%m%d%H:%M:%S"` Script 1 : File_Checkr.sh Script 2 : Pre_Validation.sh Script 3 : Testing.sh Script... (12 Replies)
Discussion started by: Deena1984
12 Replies

10. Shell Programming and Scripting

ssh fails from one server only with expecting SSH2_MSG_KEXDH_REPLY message

I have two linux servers viz 12.7.44.18 and 12.7.45.18 I wish to ssh from both these server to a destination AiX server 12.7.33.18 The ssh works from 12.7.44.18 -> 12.7.33.18 but fails from 12.7.45.18 -> 12.7.33.18 The openssl version on both linux source 12.7.44.18 and 12.7.45.18 is the... (7 Replies)
Discussion started by: mohtashims
7 Replies
TEXTDUMP(4)						   BSD Kernel Interfaces Manual 					       TEXTDUMP(4)

NAME
textdump -- textdump kernel dumping facility SYNOPSIS
options KDB options DDB DESCRIPTION
The textdump facility allows the capture of kernel debugging information to disk in a human-readable rather than the machine-readable form normally used with kernel memory dumps and minidumps. This representation, while less complete in that it does not capture full kernel state, can provide debugging information in a more compact, portable, and persistent form than a traditional dump. By combining textdump with other ddb(4) facilities, such as scripting and output capture, detailed bug information can be captured in a fully automated manner. FORMAT
textdump data is stored in a dump partition in the same style as a regular memory dump, and will be automatically extracted by savecore(8) if present on boot. textdump files are stored in the tar(5) format, and consist of one or more text files, each storing a particular type of debugging output. The following parts may be present: ddb.txt Captured ddb(4) output, if the capture facility has been used. May be disabled by clearing the debug.ddb.textdump.do_ddb sysctl. config.txt Kernel configuration, if has been compiled into the kernel. May be disabled by clearing the debug.ddb.textdump.do_config sysctl. msgbuf.txt Kernel message buffer, including recent console output if the capture facility has been used. May be disabled by clearing the debug.ddb.textdump.do_msgbuf sysctl. panic.txt Kernel panic string, if the kernel panicked before the dump was generated. May be disabled by clearing the debug.ddb.textdump.do_panic sysctl. version.txt Kernel version string. My be disabled by clearing the debug.ddb.textdump.do_version sysctl. Kernel textdumps may be extracted using tar(1). CONFIGURATION
The textdump facility is enabled as part of the kernel debugger using options KDB and options DDB. By default, kernel dumps generated on panic or via explicit requests for a dump will be regular memory dumps; however, by using the textdump set command in ddb(4), or by setting the debug.ddb.textdump.pending sysctl to 1 using sysctl(8), it is possible to request that the next dump be a textdump. If at the ddb(4) command line, the commands textdump set, textdump status, and textdump unset may be used to set, query, and clear the textdump pending flag. As with regular kernel dumps, a dump partition must be automatically or manually configured using dumpon(8). EXAMPLES
In the following example, the script kdb.enter.panic will run when the kernel debugger is entered as a result of a panic, enable output cap- ture, dump several useful pieces of debugging information, and then invoke panic in order to force a kernel dump to be written out followed by a reboot: script kdb.enter.panic=textdump set; capture on; show allpcpu; bt; ps; alltrace; show alllock; call doadump; reset In the following example, the script kdb.enter.witness will run when the kernel debugger is entered as a result of a witness violation, printing lock-related information for the user: script kdb.enter.witness=show locks These scripts may also be configured using the ddb(8) utility. SEE ALSO
tar(1), ddb(4), tar(5), ddb(8), dumpon(8), savecore(8), sysctl(8) HISTORY
The textdump facility first appeared in FreeBSD 7.1. AUTHORS
The textdump facility was created by Robert N. M. Watson. BSD
December 24, 2008 BSD
All times are GMT -4. The time now is 04:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy