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,
+ ''
++ 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
+ ''
+ 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

The implementation depends on the shell you're using,
with recent versions of bash and with ksh93 you can use the pipefail option:

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

In bash there is also an array called PIPESTATUS:

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.