problem with redirecting error from function

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 12
Reputation: chunchuwar is an unknown quantity at this point 
Solved Threads: 0
chunchuwar chunchuwar is offline Offline
Newbie Poster

problem with redirecting error from function

 
0
  #1
Oct 16th, 2007
Dear all,
I am facing trouble with calling exit status in a function.
Here is part of my experimental shell script to do remote file transfer without prompting for password.
I have created error purposefully


function iv2 {
rsync -a -e "ssh -i $RKEY" $RUSER@$RMACHINEBSDIR3/dev/* $MDIR1/dev/ 2>&1 | tee
>>$RLOG_DIR/$LOGFILE
[ $? -eq 0 ] && exit 0 || exit 1
}
if [ -f $RKEY ];then
echo `date` Source-code updation started for iv2 >> $RLOG_DIR/$LOGFILE
iv2
[ $? -eq 0 ] && echo `date` Source-code updation finished for iv2 >> $RLOG_DIR/$LOGFILE
[ $? -ne 0 ] && echo `date` Source-code updation could not be finished for iv2 >> $RLOG_DIR/$LOGFILE
fi


All the variable are defined and there is not a problem with that only I need your help in writing my exit status so that I can track error messages.This script work fine , i get error loged into my log file , here is the output of my log log file
#cat /tmp/error.log
Tue Oct 16 11:10:54 IST 2007 Source-code updation started for iv2
rsync: mkdir "/tmp/igp_32_installers_07-10-25/iv2/reader/dev" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(509) [receiver=2.6.8]

Here is part o/p when script run keeping debug on,
+ '[' -f /root/.ssh/id_rsa ']'
++ date
+ echo Tue Oct 16 11:12:44 IST 2007 Source-code updation started for iv2
+ iv2
+ rsync -a -e 'ssh -i /root/.ssh/id_rsa' 'iv2user@192.168.0.27:/var/opt/infoviewer/dev/*' /tmp/igp_32_installers_07-10-25/iv2/iv2_app/dev/
+ tee
+ '[' 0 -eq 0 ']'
+ exit 0

So you can notice that function is getting exit status from 'tee' command which I have used to log my error message. If it is exit 0 then as per my script's if test it should show me message echo `date` Source-code updation finished for iv2 and entry in log file but it's not getting, So where is mistake?

Anyhow my requirement is that when a function is called if any error generated during the execution of function that should go to log file and depending on that error I should perform some test case and simultaneously I should be able to exit from function and in my shell script can call another function.
Please provide me your help.
thanks in advance
vinod
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 54
Reputation: radoulov is an unknown quantity at this point 
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: problem with redirecting error from function

 
0
  #2
Oct 16th, 2007
Originally Posted by chunchuwar View Post
[...]
Anyhow my requirement is that when a function is called if any error generated during the execution of function that should go to log file and depending on that error I should perform some test case and simultaneously I should be able to exit from function and in my shell script can call another function.
Please provide me your help.
thanks in advance
The implementation depends on the shell you're using,
with recent versions of bash and with ksh93 you can use the pipefail option:
Shell Scripting Syntax (Toggle Plain Text)
  1. bash 3.2.25(1)$ f(){ ls "$@" 2>/dev/null||return $?;}
  2. bash 3.2.25(1)$ f no_such_file|tee log&&echo OK||echo "An error occurred: $?"
  3. OK
  4. bash 3.2.25(1)$ set -o pipefail
  5. bash 3.2.25(1)$ f no_such_file|tee log&&echo OK||echo "An error occurred: $?"
  6. An error occurred: 2
In bash there is also an array called PIPESTATUS:
Shell Scripting Syntax (Toggle Plain Text)
  1. bash 3.2.25(1)$ f(){ ls "$@" 2>/dev/null||return $?;}
  2. bash 3.2.25(1)$ f no_such_file|tee log
  3. bash 3.2.25(1)$ echo \$? is $?, PIPESTATUS is ${PIPESTATUS[@]}
  4. $? is 0, PIPESTATUS is 2 0
Last edited by radoulov; Oct 16th, 2007 at 12:17 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2255 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC