Weird Exit Status of shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird Exit Status of shell script
# 1  
Old 05-21-2013
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:
Code:
#!/bin/bash
if [ $# -lt 1 ] ; then #Check there is enough command line parameters.
        exit 1
fi

cat $1 | while read i; do #Check whether there is missing file
        `[ -e $i ]`
        if [ $? != 0 ] ; then
                exit 1
        fi
done

exit 0

Now I have a file named suite.txt whose content is
Code:
test1
test2
test3

In current directory, there are only test1 and test2 exist, however, test3 doesn't.
When executing
Code:
./check suite.txt; echo $?

It gave the 0 which means that there was no missing files. I feel so confused about that. Any ideas?
Thanks in advance!
# 2  
Old 05-21-2013
It is due to the fact that you are using a pipeline.

To see the difference, change your code to:
Code:
#!/bin/bash

if [ $# -lt 1 ] ; then #Check there is enough command line parameters.
        exit 1
fi

while read i
do
        `[ -e $i ]`
        if [ $? != 0 ] ; then
             exit 1
        fi
done < $1

exit 0

By the way, your if expression would be better written as:
Code:
while read i
do
     if  [ ! -e $i ]
     then
         exit 1
     fi
done < $1

This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 05-21-2013
In bash, the piped processes run in sub-shells. So, the second exit 1 is only affecting the sub-shell running the while loop and not your whole script.

Try a variant such as:
Code:
...
while read i; do
 `[ -e "$i" ]`
 if [ $? != 0 ] ; then
 exit 1
 fi
done < "$1"
...

Also, the command substitution is an overkill here:
Code:
...
while read i; do
 [ -e "$i" ] || exit 1
done < "$1"
...

This User Gave Thanks to elixir_sinari 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

Test with non-zero status does not exit shell

Hi there, I'm very used to use set -e to break my scripts if any command exits with a non-zero status. As a policy, I'm willingly expecting echo hello | grep a to break the script. The commands test 1 -eq $1 && echo hello exits with a non-zero status if $1 is not 1. BUT... It doesn't break... (2 Replies)
Discussion started by: chebarbudo
2 Replies

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

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

store last command exit status in variable in shell script

Hello All My req is to store the exit status of a command in shell variable I want to check whether the file has header or not The header will contain the string DATA_ACQ_CYC_CNTL_ID So I am running the command head -1 $i | grep DATA_ACQ_CYC_CNTL_ID Now I have to check if... (6 Replies)
Discussion started by: Pratik4891
6 Replies

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

8. Shell Programming and Scripting

problem in exit status of the command in a shell script-FTP

Hi All, I have developed below script for FTP a file from unix machine to another machine. ftpToABC () { USER='xyz' PASSWD='abc' echo "open xx.yy.zbx.aaa user $USER $PASSWD binary echo "put $1 abc.txt" >> /home/tmp/ftp.$$ echo "quit" >> /home/tmp/ftp.$$ ftp -ivn <... (3 Replies)
Discussion started by: RSC1985
3 Replies

9. Programming

exit status running java classpath in unix shell

I have a java classpath running inside of a unix shell script. During my testing it will error with lines that show an example like this below. java.io.FileNotFoundException error at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:129), ... (2 Replies)
Discussion started by: mmcds
2 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