Need urgent help on exit status of the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need urgent help on exit status of the script
# 1  
Old 09-09-2011
Need urgent help on exit status of the script

Guys,
I am writing a script that executes a series of commands with a function like:

_Command "ps -ef | grep java"
_Command "vmstat"
_Command "llll"

Even if one of these commands fail, my script should exit with non-zero code i.e 16.
If all commands are successful, my script should exit with 0.

My action for this requirement :
I am collecting return codes of all commands into a variable and grepping for a non-zero string in that variable. If it is found I am exiting the script with 16 otherwise exit the script with 0.But , my code is not working in case the variable is having all zeros' .

example:
Code:
sriram] var="0 0 0 0"
sriram] echo $var | grep -qv 0
sriram] $?
ksh: 1:  not found.
sriram] var="0 0 0 127"
sriram] echo $var | grep -qv 0
sriram] $?
ksh: 1:  not found.

In both cases grep is returning 1.
If it var contains all zeros, then my script should exit with 0.

This is my actual script:
Code:
#Command execution function
_Command()
{
   _Report "BEGIN Command : $@ "
    #Executing command and redirecting stderr to devnull
    output=$(eval $@)
    #Exit status of the command
    stat=$?
    echo $output
    echo "++ $stat ++"

    if [[ $stat -ne 0 ]] ; then
        #collecting all return codes of commands into a variable
                Returns=$Returns" "$stat
        _Report "$@ not successful"
        _Report "END"
    else
                Returns=$Returns" "$stat
        _Log "$output"
        _Report "END"
    fi
}

#List of commands to be run for unix swap space problem

        _Report "##  memory size used by each of the running processes ##"
        _Command 'ps -eo vsz,pid,args | sort -n'

    _Report "## memory that is locked but not used ##"
    _Command 'ipcs -ma | grep -w -E "0|NATTCH" '

        _Report "## Check for virtual memory statistics ##"
        _Command "vmstat"

        _Command "ps -ef | grep micro"


#Search for nonzero code in all the exitcodes of commands
echo $Returns
echo $Returns | grep -qv 0

#If one of the commands fail, autocheck script will return 16
[ $? = 1 ] && exit $Error || exit $Success


Please help or suggest any alternatives...
Many thanks in advance,

SriramSmilie
# 2  
Old 09-09-2011
Code:
var="0 0 0 0"
echo $var | grep -q "0 0 0 0"
echo $?
0

var="0 0 0 127"
echo $var | grep -q "0 0 0 0"
echo $?
1

var="0 0 0 0"
echo $var | grep -qv "0 0 0 0"
echo $?
1

var="0 0 0 127"
echo $var | grep -qv "0 0 0 0"
echo $?
0

you missed echo in your testing
-v is for invert selection

--ahamed
# 3  
Old 09-09-2011
thanks ahmed,

but i cannot hard "0 0 0 0".... as i can run even 10 commands in the same script..
so i have to grep for non-zero exit codes.

Thanks,

SriramSmilie
# 4  
Old 09-09-2011
Just add a flag variable and change right after a command fails.
At the end just check if that variable value has changed from the beginning:

Code:
_flag=0
...
command  ... || _flag=16
...
command ... || _flag=16
....
exit $_flag

This User Gave Thanks to radoulov For This Post:
# 5  
Old 09-09-2011
Code:
echo $var | sed 's/0//g' | grep -qv 0; echo $?

Not sure if it is the best solution. But it will serve the purpose I guess.
Even if you have 0 in a non-zeo exit code, it shouldn't be a problem.

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 09-09-2011
thanks ahamed and rod... both solutions work.. however i got another idea just now...

I am not collecting succesful commands, instead i am collecting only non-zero codes...
so, i f all commands are succesful, $var contains " " or $var contains "127 ".

So, I wrote this to handle, it is working not efficient though, am i right:

my $var contains either var=" 127 255" or var=" "..

Code:
#If one of the commands fail, autocheck script will return 16
if  $var 2>/dev/null ; then
   exit $Success
else
   exit $Error
fi

# 7  
Old 09-09-2011
Quote:
Originally Posted by ahamed101
Code:
echo $var | sed 's/0//g' | grep -qv 0; echo $?

Not sure if it is the best solution. But it will serve the purpose I guess.
Even if you have 0 in a non-zeo exit code, it shouldn't be a problem.
Alternatively, he could have grepped for a non-zero digit. Although, if each individual exit status is not required, radoulov's suggestion is probably best. And since it seems that the command sequence is known at "compile" time, there's really no need for eval.

Regards,
Alister

---------- Post updated at 11:05 AM ---------- Previous update was at 11:02 AM ----------

Quote:
Originally Posted by sriramperumalla
my $var contains either var=" 127 255" or var=" "..

Code:
#If one of the commands fail, autocheck script will return 16
if  $var 2>/dev/null ; then
   exit $Success
else
   exit $Error
fi


That won't work at all. You need to use test/[.

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inner script run and its exit status

Main Script #!/bin/ksh echo "Maimn script" ./clocal/www/web-data/WAS/WebSphere7/scripts/DealerLocator/Scripts/secondscript.ksh echo "$? = status" Sdecond Script #!/bin/ksh echo "In second SCript" exit 1 Output: Maimn script ./testmain.ksh:... (4 Replies)
Discussion started by: dineshaila
4 Replies

2. Shell Programming and Scripting

Exit status in the script

Hi all, I am trying to use a script (a.sh) which is calling another script(b.sh). And I want to use the exit code(set by me) of b.sh in a.sh. I am using this in b.sh #!/bin/sh <-- code --> if ; then exit 0 else exit 1 fiBut... (2 Replies)
Discussion started by: Raj999
2 Replies

3. Shell Programming and Scripting

Weird Exit Status of shell script

I have a script named check which will read the content of a file and check wether those files exist in the current directory. If so it will have the exit status of 0, otherwise it will have 1. check script: #!/bin/bash if ; then #Check there is enough command line parameters. exit 1... (2 Replies)
Discussion started by: Ray Sun
2 Replies

4. Shell Programming and Scripting

Exit status of the ksh Script

Hi Im trying to write a script that will archive some file using java program.Below is the part of the script that I use and my problem is that the script always return with status 0.Below is part of my script(end part) purge.ksh echo "No of files before tar :... (4 Replies)
Discussion started by: saachinsiva
4 Replies

5. Shell Programming and Scripting

exit status from the script is always 0

Hi , I have a bash script , which does the network configuration. Messages from this script are dumped on console as well as stored in a log file . This script is invoked from a C code using system call . The script returns different exit code , to indicate different error cases. The... (1 Reply)
Discussion started by: abhirai
1 Replies

6. Shell Programming and Scripting

How to test for the ssh exit status in script?

Hello; I regularly run monitoring scripts over ssh to monitoring scripts But whenever a server is hung or in maintenance mode, my script hangs.. Are there anyways to trap exit status and be on my way ?? Looked at the ssh manpage and all I can see is a "-q" option for quiet mode .. Thank... (2 Replies)
Discussion started by: delphys
2 Replies

7. Shell Programming and Scripting

HELP WITH .ksh script converting the exit status

Hi Can someone help me please? In a standard UNIX .ksh script, if you have the exit status..say 5...what line do you have to enter into the script for this number to be automatically converted to its actual exit reason by looking up the exit status file...wherever that is? thanks angus (1 Reply)
Discussion started by: angusyoung
1 Replies

8. Shell Programming and Scripting

urgent -read exit status

i have written a shell script that invokes main class of a java prg. the java prg does a System.exit(0) or (1) based on condition. how can i read or check the status in unix.... (4 Replies)
Discussion started by: iamcool
4 Replies

9. 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

10. Shell Programming and Scripting

checking exit status of a shell script

Hi, I have tried with the following code; if ;then echo "Failure." else echo "Success." fi to test the exit status of the test.ksh shell script. But whatever is the exit status of the test.ksh shell script Failure. is always printed. Please help. regards, Dipankar. (2 Replies)
Discussion started by: kdipankar
2 Replies
Login or Register to Ask a Question