Exit status of the ksh Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit status of the ksh Script
# 1  
Old 08-09-2012
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

Code:
                echo "No of files before tar : $noOfFilesDir"
                tar -cf $line/$logDate.tar `find $line -type f ! -name "*.tar"`
                noOfFilesTar=`tar -tf $line/$logDate.tar | wc -l`
                echo "No of files in tar : $noOfFilesTar"
                if [ $noOfFilesDir -ne $noOfFilesTar ]
                then
                        echo "Some of the files could not be archived under $line.Please investigate"
                        retVal=1
                else
                        find $line -type f ! -name "*.tar" -exec rm -f {} \;
                fi
        done <"$file"
        rm -f $TAR_LIST_FILE_NAME
fi
echo "\nUnix script exited with code : $retVal"
exit $retVal
) 2>&1 | tee $APP_DIR/logs/purgeFiles_$logDate.log

I call this script from another script and then check the return status and it always return 0 even if the retVal is 1.

Calling script:

ksh purge.ksh
echo $?

The status code is always 0.
Please help as I need to redirect the ouput of the whole script to screen and also to the file and exit the script based on retVal.

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 08-09-2012 at 12:10 AM..
# 2  
Old 08-09-2012
If this never gets executed then it(retVal) will be what you initialized it to:
Code:
if [ $noOfFilesDir -ne $noOfFilesTar ] then
  echo "Some of the files could not be archived under $line.Please investigate"
  retVal=1



Also try exiting with a "1" like this and see what you get:

Code:
exit 1

# 3  
Old 08-09-2012
Hi

Sorry not for being very clear at first.The java code exit with code 1 and I can see the out put "Unix code exited with output 1" but still $? from calling script shows output code as 0.
# 4  
Old 08-09-2012
As far as i can see from the snippet you set "retVal=1" within a loop. This loop might get executed several times any maybe there is a "retVal=0" above which gets executed later on in another pass of the loop.

I hope this helps.

bakunin
# 5  
Old 08-09-2012
Code:
(
       echo "some"  
       exit 1
) 2>&1   | tee somefile.tmp

After this ? is 0 if tee works (access to write file). But if you remove pipe tee, then you get that exit code which you are trying.
? include always the last command exit status. ( ) is subprocess, so exit exits only that subprocess. After that you have tee.
If you look somefile.tmp, it include word some. So tee works fine = exit 0.
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 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

5. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: sriramperumalla
7 Replies

6. Shell Programming and Scripting

exit status from ksh script exec from java using runtime

how do i get the exit status from a ksh or perl script executed in a java program using Runtime? (1 Reply)
Discussion started by: twk
1 Replies

7. Shell Programming and Scripting

Exit status always zero in KSH from Oracle Scheduler

Hi, Running Oracle Scheduler 11g2 on an AIX 6.01. I want to run a shell script (called "external program" in Oracle terminology) which executes some commands and react on the outcome using the exit status $?. For example a "cat thisfiledoesnotexist" and then get the $? which should be not zero.... (6 Replies)
Discussion started by: hermanmol
6 Replies

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

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

Need Clean Exit from KSH Status Loop

I have a ksh loop that monitors front panel key postitions. I need a keystroke or something to break out of the loop without exiting the script. Code is: #!/bin/ksh while true do POST=$(./keystat2 | nawk '{print $1}') if ]; then ... (2 Replies)
Discussion started by: ScottKe
2 Replies
Login or Register to Ask a Question