Exit 1 doesn't work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit 1 doesn't work
# 1  
Old 05-05-2017
Exit 1 doesn't work

Hi
Code:
tail -f $PROGPATH/NBU_pgbaserestore_$1.log | while read LOGLINE
        do
        if [[ $LOGLINE =~ .*INF\ -\ Status.* ]] && ! [[ $LOGLINE =~ .*success.* ]]
        then
        date "+%d.%B.%Y %H:%M:%S"
        echo "ERROR: NBU"
        echo "$LOGLINE"
        TAILKILL=$(pgrep -P $$ -x tail)
        kill -9 $TAILKILL
        exit 1
        elif [[ $LOGLINE =~ .*INF\ -\ Status.*success.* ]]
        then
        date "+%d.%B.%Y %H:%M:%S"
        echo "NBU: $LOGLINE"
        TAILKILL=$(pgrep -P $$ -x tail)
        kill -9 $TAILKILL
        fi
        done

And exit 1 there will be skipped. Why?
# 2  
Old 05-05-2017
This is because the exit command is executed in a subshell (the right hand side of the pipeline). So the subshell is exited, but not the parent shell.

If you were to use ksh (Korn Shell) or zsh , then it would work because these shells do not execute the RHS of a pipeline in a subshell (but rather in the foreground)


--
In bash you could try adding this after the done statement:
Code:
RC=$?
if [ $RC -ne 0 ]; then
 exit $RC
fi


Last edited by Scrutinizer; 05-05-2017 at 03:13 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-05-2017
Your solution works, thnx
# 4  
Old 05-05-2017
Or simply
Code:
...
done || exit

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

ksh script doesn't exit in Jenkins

I have a ksh script which runs on the commandline of Jenkins. Script only has commands and few echo statements.I have added set -e as well. But the issue is that , script completes but donot exits on Jenkins commandline. whereas when I run it on unix, it executes perfectly and exits.I need to... (1 Reply)
Discussion started by: dreams619
1 Replies

2. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

3. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

My if statement doesn't work why?

I have the following and for some reason I can't have two options together. I mean if I choose -u and -p it won't work... why? #!/bin/bash resetTime=1 mytotalTime=0 totalHour=0 totalMin=0 averagemem=0 finalaverage=0 times=0 function usage() { cat << EOF USAGE: $0 file EOF } (10 Replies)
Discussion started by: bashily
10 Replies

5. Shell Programming and Scripting

Lipo doesn't work

Hi guys, Am using lipo to merge ppc and i386 version of a static/dylib file based on "file type to load". I am working on Mac OS 10.5.6 and new to shell scripting. Please help me out. This is my code. echo "This file combine ppc and i386 file to form universal library" echo "source... (4 Replies)
Discussion started by: vishwesh
4 Replies

6. UNIX for Advanced & Expert Users

remsh doesn't work

Hi, I need to use remsh inside a ksh script. The script would remsh to another machine (maybe different OS) and then execute commands. A Simple Script: #!/usr/bin/ksh remsh sun7656 -l myuser "cd /user.3/MyFolder; ls -lart" But this gives me the error: permission denied I also... (4 Replies)
Discussion started by: som.nitk
4 Replies

7. Shell Programming and Scripting

ls -d doesn't work on Solaris

Hello, the ls -d command to only list directories in a directory doesn't seem to work on Solaris and the man command says to use that combination: ls -d Anyone have the same problem and find a resolve? Thanks BobK (9 Replies)
Discussion started by: bobk544
9 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

sed doesn't work

Hello I' m confused a bit. I want to replace string "&amp" with "&" using this command. sed 's/&amp/&/g' and it doesn't work. Nothing happens. On the other side this works: sed 's/&amp/@/g' or sed 's/&amp/^/g' !!! Can somebody help please? Thanks (3 Replies)
Discussion started by: billy5
3 Replies

10. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question