2

I have run at below script in HP/UX and get that output:

Exiting #1
Exiting #2

But I expect that output:

Exiting #1

Script file:

data="aaa; bbb; ccc"

echo $data | while IFS=';' read -ra array; do
    echo "Exiting #1"
    exit -1
done


echo "Exiting #2"

exit 0

How can I solve this problem? Thanks.

1
  • 1
    Be aware that -1 is not a valid exit status for a process; it must be a non-negative value. Commented Dec 27, 2013 at 14:51

2 Answers 2

4

Because of the pipe, the command in the loop run in a subshell. When you call exit, only the subshell is terminated, and not the parent process.

You can overcome this by using shopt -s lastpipe, by using process substitution

while IFS=';' read -ra array; do
    echo "Exiting #1"
    exit -1
done < <(echo "$data")

or by using a here-string

while IFS=';' read -ra array; do
    echo "Exiting #1"
    exit -1
done <<< "$data"
Sign up to request clarification or add additional context in comments.

Comments

2

When you do exit -1 inside the loop, it only exits the subprocess that was created due the pipe. Re-write it to use here-string:

 while IFS=';' read -ra array; do
    echo "Exiting #1"
    exit -1
done <<< ${data}

Now, you'll get the expected output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.