Test with non-zero status does not exit shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test with non-zero status does not exit shell
# 1  
Old 02-19-2016
Test with non-zero status does not exit shell

Hi there,

I'm very used to use
Code:
set -e

to break my scripts if any command exits with a non-zero status.

As a policy, I'm willingly expecting
Code:
echo hello | grep a

to break the script.

The commands
Code:
test 1 -eq $1 && echo hello

exits with a non-zero status if $1 is not 1. BUT... It doesn't break the script.

Does anyone know why?

Cheers
Santiago
# 2  
Old 02-19-2016
My man page tells me this:
Quote:
Originally Posted by man bash
...
-e Exit immediately if a pipeline (which may consist of a
single simple command), a list, or a compound command
(see SHELL GRAMMAR above), exits with a non-zero staâ
tus. The shell does not exit if the command that fails
is part of the command list immediately following a
while or until keyword, part of the test following the
if or elif reserved words, part of any command executed
in a && or || list except the command following the
final && or ||
, any command in a pipeline but the last,
or if the command's return value is being inverted with
!. If a compound command other than a subshell returns
a non-zero status because a command failed while -e was
being ignored, the shell does not exit. A trap on ERR,
if set, is executed before the shell exits. This option
applies to the shell environment and each subshell enviâ
ronment separately (see COMMAND EXECUTION ENVIRONMENT
above), and may cause subshells to exit before executing
all the commands in the subshell.

...
This User Gave Thanks to cero For This Post:
# 3  
Old 02-19-2016
Much clearer.
The output of help set did not give me that much information.
Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Test exit status of last cmd via ssh

see below for a housekeeping script which constructs an ssh cmd using some server/path/sudo info found in $HRINST. the script should hop to each server and if it finds a file to cleanup, moves it to the archive dir if there is nothing to move, it should report so and email the output ... (3 Replies)
Discussion started by: jack.bauer
3 Replies

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

4. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

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

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

7. Shell Programming and Scripting

How to Create a shell script to test the telnet connection status

Hi friends, I'm newbie to shell script. I wanted to create a shell script which able to write a result for all the telnet connection status. For example, from this machine I want to test the telnet connection (total 100+ servers) with this machine. Any idea how to write this shell script?... (16 Replies)
Discussion started by: yhcheong
16 Replies

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

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

10. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies
Login or Register to Ask a Question