LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-06-2013, 05:26 AM   #1
Rasna
LQ Newbie
 
Registered: Jul 2012
Location: Bangalore
Posts: 5

Rep: Reputation: Disabled
Post stderr + logging and exit status


How to get a shell commands stderr to screen and also to a log file and the exit status of the command (that is $?).
Any idea how to get all these?
I tried "command 2>&1 | tee error.log"
but that time I didnt get the echo $? status properly!
Thanks in advance
Rasna
 
Old 06-06-2013, 07:15 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
If you want to show stderr on screen then don't redirect it to any file. And $? will give you exit status of last command run, so explain it little more that what exactly you did.
Also take a note that, 2>&1 will redirect sterr as well stdout to specified file, so if you just want to redirect stderr, then use only 2 > filename.
 
Old 06-06-2013, 07:48 AM   #3
Rasna
LQ Newbie
 
Registered: Jul 2012
Location: Bangalore
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hi,
actual concern is,
I wanted to print the stderr of the command execution and also wanted to compare this with an expected "message".
so actual flow will be
> check for the command exist status (using $?)
> then if exit status is not zero , I need to check for the expected "message" in the stderr and return this case differently.[Here stderr needed to be printed to the screen also]

my logic is:
>command 2>error.log
>if $? is not zero will do cat of error.log and then do grep for the "message" in error.log

Last edited by Rasna; 06-06-2013 at 07:53 AM.
 
Old 06-06-2013, 09:08 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
As said previously, without seeing the script, it's not easy to predict that what you want to do. However, you can try in following way:
Code:
#!/bin/bash
command 2> error.log
if [ $? -ne 0 ] ; then
cat error.log | grep <error_msg>  ## grep error message if exit status of previous cmd is not 0.
fi
 
1 members found this post helpful.
Old 06-06-2013, 09:12 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,096
Blog Entries: 10

Rep: Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522Reputation: 1522
Anything you can write in a script file you can send to the command line, they are the same thing.

So you can type
Code:
ls; if [ $? -eq 1 ]; then echo "ERROR"; fi; exit
in the command line, or you can write that in a script.

However you're doing this, you can place an "if" statement in there to evaluate the command result.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syslog: prepend level when logging to stderr Meson Programming 0 09-02-2012 12:48 PM
bash logging stdout plus stderr freeindy Programming 6 01-29-2009 10:16 AM
Exit status vedanu Programming 4 06-23-2007 06:13 PM
exit status naflan Programming 2 10-22-2004 01:22 PM
error exit status 30 debian_dummy Debian 2 09-21-2004 11:26 PM


All times are GMT -5. The time now is 08:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration