Setting script exit code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting script exit code
# 1  
Old 04-06-2010
Setting script exit code

Code:
#!/bin/ksh
row=`sed '1!G;h;$!d' file1.xml | head -2| tail -1`
echo "$row" | awk -F"[;: ]" '{$esum=$5}'

row=`sed '1!G;h;$!d' file2.xml | head -2| tail -1`
echo "$row" | awk -F"[;: ]" '{$isum=$5+$19}'

echo "Exp:$esnum"
echo "Imp:$isum"
if [$esum -eq $isum]
then
 echo "Matched"
else
 echo "Not matched"
fi


a) In the above code my values for esum & isum are becomin 0 at the if condition. How to retain the values thru out the program?
b) Instead of "Matched", exit code should be set to "0" and in ELSE part exit code shuold be set to "1". How to do this?

Please help....

Last edited by skyineyes; 04-06-2010 at 10:51 AM..
# 2  
Old 04-06-2010
IF (condition 1)
set exit code 0
else
set exit code 1


you may not need to mention "set exit code 0"

IF ( Condition 1)
then
do something
exit 234
else
do something else
exit 242


Wherein you know the exit status post execution of the script

./script
echo $?

so if it is 234 ==> condition met
or
242 ==> condition failed
# 3  
Old 04-06-2010
Network

Quote:
Originally Posted by maverick_here
IF (condition 1)
set exit code 0
else
set exit code 1


you may not need to mention "set exit code 0"

IF ( Condition 1)
then
do something
exit 234
else
do something else
exit 242


Wherein you know the exit status post execution of the script

./script
echo $?

so if it is 234 ==> condition met
or
242 ==> condition failed
There are additional queris. Could you please have a look again?
# 4  
Old 04-06-2010
Oops, the varaible used in the awk , won't be retained outside.

You should use something like this:

Code:
row=`sed '1!G;h;$!d' file1.xml | head -2| tail -1`
esum=`echo "$row" | awk -F"[;: ]" '{$esum=$5}'`

row=`sed '1!G;h;$!d' file2.xml | head -2| tail -1`
isum=`echo "$row" | awk -F"[;: ]" '{$isum=$5+$19}'`
echo "Exp:$esnum"
echo "Imp:$isum"
if [ $esum -eq $isum ]
then
 echo "Matched" ## you can use exit 0
else
 echo "Not matched" ## you can use exit 1
fi


Last edited by panyam; 04-06-2010 at 10:59 AM.. Reason: code tags added
# 5  
Old 04-06-2010
Sorry for the goof up Smilie
# 6  
Old 04-06-2010
Quote:
Originally Posted by panyam
Oops, the varaible used in the awk , won't be retained outside.

You should use something like this:

The code is not working. Still its printing blank i.e the values are still not accesible outside. do we need any modification????

Last edited by skyineyes; 04-06-2010 at 11:16 AM..
# 7  
Old 04-06-2010
Quote:
Originally Posted by skyineyes
The code is not working. Still its printing blank i.e the values are still not accesible outside. do we need any modification????
You did not print the variables in the awk , so it should be :

Code:
 
row=`sed '1!G;h;$!d' file1.xml | head -2| tail -1`
esum=`echo "$row" | awk -F"[;: ]" '{print $5;}'`

row=`sed '1!G;h;$!d' file2.xml | head -2| tail -1`
isum=`echo "$row" | awk -F"[;: ]" '{print $5+$19}'`

echo "Exp:$esum"
echo "Imp:$isum"
if [ $esum -eq $isum ]
then
echo "Matched" ## you can use exit 0
else
echo "Not matched" ## you can use exit 1
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit code 267 from shell script

Hi, We have a problem in Linux (GNU/Linux 3.10.0-693.1.1.el7.x86_64) with a shell script returning 267 as return code. The script, load_flag.sh is called from main_script.sh (both script samples given below). The exit code from load_flag.sh is used to decide whether to continue execution of... (1 Reply)
Discussion started by: Arunnath
1 Replies

2. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

4. Shell Programming and Scripting

Exit code from piping in unix shell script

Hi , I have following code in my shell script : "$TS_BIN/tranfrmr" "${TS_SETTINGS}/tranfrmr_p1.stx" "${TS_LOGS}/tranfrmr_p1.err" | ( "$TS_BIN/cusparse" "${TS_SETTINGS}/cusparse_p2.stx" "${TS_LOGS}/cusparse_p2.err" | ( "$TS_BIN/tsqsort" "${TS_SETTINGS}/srtforpm_p3.stx"... (8 Replies)
Discussion started by: sonu_pal
8 Replies

5. Shell Programming and Scripting

Send correct exit code from child script back to parent

Hello all; hope someone can help me cause I am going crazy trying to find a solution for (what I think is simple) issue...looked hard up and down this forum and tried several "solutions" with no avail...so here's my issue: I have this (parent) script: copylsofdcmcadefttosftpwithmove.sh ... (3 Replies)
Discussion started by: gvolpini
3 Replies

6. Shell Programming and Scripting

Expect script: obtain the exit code of remote command

Hi all, I'm trying to run the sipp simulator in crontab but after some attempt I came to the conclusion that for some reason this isn't possible (maybe due to sipp interactive nature). This is confirmed by these posts. Now I'm trying to launch sipp from an expect script that runs in crontab. ... (0 Replies)
Discussion started by: Evan
0 Replies

7. Shell Programming and Scripting

fuser exit code different when script is run from cron

Hi, I am writing a bash script (running on Centos 5.4) to process video (.MTS) files which may have appeared in a certain directory. The files will be dragged and dropped there from a Windows box using Samba, and the script is to check periodically (i.e. run from cron) whether any new .MTS... (0 Replies)
Discussion started by: Palamedes
0 Replies

8. Shell Programming and Scripting

Exit shell after setting executable to run?

Hi, I have an executable file that has a rather long and tedious process to complete. How would I launch the executable using the shell, and then exit the shell while leaving the executable to run in the background? (1 Reply)
Discussion started by: pcwiz
1 Replies

9. Shell Programming and Scripting

Passing exit code to calling script

In production I need to pass an exit code from a script that is being called 3 or 4 layers deep. I've created test scripts to play with it until I get it right. As you can tell, this is not my normal job. Perhaps I should have entered this in UNIX for Dummies section. Anyway, I keep losing the... (2 Replies)
Discussion started by: debbiekuch
2 Replies

10. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question