| | |
problem with redirecting error from function
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
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@$RMACHINE
BSDIR3/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
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@$RMACHINE
BSDIR3/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
•
•
•
•
[...]
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
with recent versions of bash and with ksh93 you can use the pipefail option:
Shell Scripting Syntax (Toggle Plain Text)
bash 3.2.25(1)$ f(){ ls "$@" 2>/dev/null||return $?;} bash 3.2.25(1)$ f no_such_file|tee log&&echo OK||echo "An error occurred: $?" OK bash 3.2.25(1)$ set -o pipefail bash 3.2.25(1)$ f no_such_file|tee log&&echo OK||echo "An error occurred: $?" An error occurred: 2
Shell Scripting Syntax (Toggle Plain Text)
bash 3.2.25(1)$ f(){ ls "$@" 2>/dev/null||return $?;} bash 3.2.25(1)$ f no_such_file|tee log bash 3.2.25(1)$ echo \$? is $?, PIPESTATUS is ${PIPESTATUS[@]} $? is 0, PIPESTATUS is 2 0
Last edited by radoulov; Oct 16th, 2007 at 12:17 pm.
![]() |
Similar Threads
- error C2064: term does not evaluate to a function taking 1 arguments (C++)
- no newline at end of file error (C++)
- Problem in displaying returned value from function (C#)
- Function Error Message Help (C++)
- Big Problem, Generic Error (VB.NET)
- Visual Runtime Error, Sound Problem, Disabled Norton and more! (Windows NT / 2000 / XP)
- Problem with XP and DNS error (Viruses, Spyware and other Nasties)
Other Threads in the Shell Scripting Forum
- Previous Thread: string manipulation
- Next Thread: let me know the meaning of the script
Views: 2255 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Shell Scripting





