×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

collecting exit code on variable
2

collecting exit code on variable

collecting exit code on variable

(OP)

why red echo does not return '2' while green one does?

CODE --> case

$ unset C
$ { ls missing 2>&1;C=$?; }
ls: 0653-341 The file missing does not exist.
$ echo $C
2
$ unset C
$ { ls missing 2>&1;C=$?; }|sed s/^/error:\ /g
error: ls: 0653-341 The file missing does not exist.
$ echo $C

$ 

RE: collecting exit code on variable

Hi

Because the pipe forces the running of the list in a subshell :

CODE --> command line

master # { ps; }
  PID TTY          TIME CMD
18232 pts/7    00:00:00 bash
18496 pts/7    00:00:00 ps

master # { ps; } | cat
  PID TTY          TIME CMD
18232 pts/7    00:00:00 bash
18500 pts/7    00:00:00 bash
18501 pts/7    00:00:00 cat
18502 pts/7    00:00:00 ps 

See ? Another bash process appeared and the list will be executed by that one. That includes setting the C variable in that second bash's environment, which vanishes as soon the list's execution ends.

Regarding your code, I would rewrite it without trying to pass $? through a variable, using PIPESTATUS instead :

Quote (man bash)

       PIPESTATUS
              An  array  variable (see Arrays below) containing a list of exit
              status values from the processes in  the  most-recently-executed
              foreground pipeline (which may contain only a single command). 

CODE --> command line

master # ls missing 2>&1 | sed s/^/error:\ /g
error: ls: cannot access missing: No such file or directory

master # echo "${PIPESTATUS[0]}"
2 

Feherke.
feherke.ga

RE: collecting exit code on variable

(OP)
thank you.

btw. in ksh93g+, which does not have PIPESTATUS I found another trick for getting exit code

CODE --> code

$ ls missing 2>&1|sed s/^/error:\ /g
error: ls: 0653-341 The file missing does not exist.
$ echo $?
0
$ set -o pipefail
$ ls missing 2>&1|sed s/^/error:\ /g
error: ls: 0653-341 The file missing does not exist.
$ echo $?
2
$ 

in older ksh it is probably not such trivial.
maybe by doing/collecting exit code on output (echo $?) and processing it later

RE: collecting exit code on variable

Hi

Doh. And pipefail exists in Bash too. I never used it. That is a great trick, thanks.

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close