Check for exit status


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for exit status
# 1  
Old 01-18-2010
Bug Check for exit status

Hi I have following code
I want If whole code executes successfully then return true If found any error then print the error

I tried if [ "$?" == "0" ]; then
But this checks only for the just upper line execution

Code:
 
#!/bin/bash
PATH1=/var/log/mysql
PATH2=/home/ankur/log
FILE1=mysql-bin.index
mysqladmin -u root -p'test!@#' flush-logs
printf "%s\n" $(comm -3 $PATH1/$FILE1 $PATH2/.FILE2)>$PATH2/.FILE3
sed -e '$d' $PATH2/.FILE3 >$PATH2/.FILE4
awk '{ a[NR]=$0 } END { for(i=NR; i; --i) print a[i] } ' $PATH2/.FILE4 > $PATH2/.FILE5
sleep 1
echo "Today is $(date)"
echo "--------------------"
for i in `cat $PATH2/.FILE5`;
do
j=(${i/*\//})
#echo "$j"
if [ ! -f $PATH2/$j ];
then cp -fr  $PATH1/$j $PATH2/$j ;
gzip -9  $PATH2/$j
echo "Copying binlogs : $j"
#else
#echo "ankur"
#cat FILE1 > FILE2
#exit 0
fi  ;
done
cat $PATH1/$FILE1 > $PATH2/.FILE2
sed -e '$d' $PATH2/.FILE2 >$PATH2/.TEMP
cat $PATH2/.TEMP>$PATH2/.FILE2
exit 0

# 2  
Old 01-18-2010
Put the piece of code in a function then check the status of function.
# 3  
Old 01-18-2010
Bug

Quote:
Originally Posted by dinjo_jo
Put the piece of code in a function then check the status of function.
Hi I put the function as follow

Code:
 
#!/bin/bash
checkstatus () {
..
.
}
checkstatus;
if [ "$?" == "0" ]; then
echo STATUS=${?}
else
echo "failed"
fi
exit 0

But If I got any error except last line of code, exit status =0 always

I want If any error found in function at any line then function terminates from there only and show error
# 4  
Old 01-18-2010
You want to check for errors on each line but do not want to check for errors on each line, shell remembers the last command status, you rather have to pipeline the process.
# 5  
Old 01-18-2010
Two ways :
1) Put your commands in nested if ... then ... fi statements.
2) Chain your commands with && like
Code:
command1 && command2 && command3
# Is similar to
if command1
then if command 2
       then command3
       fi
fi

That will return "0" only if the 3 commands executed successfully. The other thing is that command2 will only be executed if command1 executed successfully, and so on. It's almost preferable to avoid executing a command if the previous operation didn't succeed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

3. Shell Programming and Scripting

Check the exit status in a pipe call

Guys, I have a problem :confused: and I need some help: I've to process many huge zip files. I'd code an application that receive the data from a pipe, so I can simple unzip the data and send it (via pipe) to my app. Something like that: gzip -dc <file> | app The problem is: How can I... (7 Replies)
Discussion started by: Rkolbe
7 Replies

4. Shell Programming and Scripting

How to check exit status of unset variables

Hi All, Is there any way to check exit status of unset variables? In the following code PathX is not set and the script terminates without checking exit status. #!/bin/bash Path="/tmp/log" cd ${PathX:?} if ;then echo "Exit Status : non zero" else echo "Exit Status :... (2 Replies)
Discussion started by: sussus2326
2 Replies

5. Shell Programming and Scripting

check exit status of bg scripts

HI All, I am running one shell script, in that script i am calling 4 scripts in the background. abc.ksh & efg.ksh & xky.ksh & mno.ksh & please let me know, how could i find the success and failure of each script. i Cannot use $?, because i want to run all the scripts in parellel. ... (2 Replies)
Discussion started by: javeed7
2 Replies

6. Shell Programming and Scripting

Exit status

I'm preparing for exam and one of exams is to write own test command... I wonder if in unix is a command which just returns exit code you specify.. I know I can easily write a function like this: exStatus() { return $1 } -> my question is rather theoretical thank you! (9 Replies)
Discussion started by: MartyIX
9 Replies

7. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

8. Shell Programming and Scripting

check exit status - Expect Script

from my main script, i am calling an expect script. there are a lot of conditions in the Expect script and it can have any exit value based on success or failure of the Expect Script. how can i check the exit status of Expect scritp in the main script. (1 Reply)
Discussion started by: iamcool
1 Replies

9. UNIX for Dummies Questions & Answers

how to check exit status in awk script

Hi, I have a main program which have below lines - awk -f test.awk inputFileName - I wonder how to check status return from awk script. content of awk script: test.awk --- if ( pass validation ) { exit 1 } else { (1 Reply)
Discussion started by: epall
1 Replies

10. Shell Programming and Scripting

check the status and send an email with status

Hi, We have a text file which has the following data. ISA~00~ ~00~ ~ZZ~VISTN ~ZZ~U1CAD ~051227~183 7~U~00200~000011258~0~P~< GS~FA~EE05J~U1CAD~051227~1831~000011258~X~002002 ST~997~0001 AK1~SH~247 AK2~856~2470001 AK5~A AK2~856~2470002 AK5~A... (3 Replies)
Discussion started by: isingh786
3 Replies
Login or Register to Ask a Question