LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-27-2013, 07:29 AM   #1
idaham
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Rep: Reputation: 0
Print stdout and stderr to screen + log file and catch any errors in shell script


Hello!
I'm writing a shell script that executes a command. I want to save both stdout and stderr to the same log file + print to sceen and at the same time catch if there is an error. What I have so far is:
Code:
set -o pipefail
errorsFound=0
command arg1 arg2 2>&1 | tee logFile.log || errorsFound=1
if [[ $errorsFound == 1 ]]; then 
	echo "command failed!"
	exit 1 
fi
When I run the shell script the logFile.log is created and seems to contain all the output information. However, the variable "errorsFound" always turns to 1, even though there are no errors from the command. Have I set this up wrong?
Thanks in advance!
 
Old 03-27-2013, 08:25 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,831

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
yes, tee will eat up the error code.
try the following:
Code:
set -o pipefail
errorsFound=0
{
command args 2>&1 || {
    echo command failed
    exit 1
}
} | tee logfile
 
1 members found this post helpful.
Old 03-28-2013, 04:32 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,280

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Incidentally, '==' is a string comparator; you'd want '-eq'
http://tldp.org/LDP/abs/html/comparison-ops.html
 
Old 03-28-2013, 06:43 AM   #4
idaham
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Thank you pan64, your solution worked just fine! I used the code below
Code:
set -o pipefail
errorsFound=0
{
command args 2>&1 || { errorsFound=1; }

if [[ $errorsFound == 1 ]]; then { echo "command failed"; exit 1; } fi
} | tee logfile
 
  


Reply

Tags
error, logfile, print, stderr, stdout


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] stderr stdout to a log file troubles metallica1973 Linux - Software 3 11-12-2011 04:28 PM
Can't seem to catch some output from a program - not stdout or stderr. uhh.. help? BrianK Programming 2 01-30-2010 11:53 AM
Shell script stdout, stderr and stdin solo9300 Linux - General 6 12-29-2009 12:33 AM
shell script: how to send stdout one place & stderr another? BrianK Programming 8 09-21-2007 06:57 AM
Shell script - stdout & stderr to terminal and file jantman Linux - Software 1 12-07-2006 04:34 PM


All times are GMT -5. The time now is 12:58 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration