Access return code of last command in AWK

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Command not found should produce return code 127:
$ foo; echo $?
bash: foo: command not found...
127
I tried to assign $? to variable rc and then print it, but RC is always 0.
$ foo | awk -v rc="$?" 'BEGINprint rc'
0
bash: foo: command not found...
Found out that it will print correct RC only in this case:
$ qazqaz
bash: qazqaz: command not found...
$ foo | awk -v rc="$?" 'BEGINprint rc'
127
bash: foo: command not found...
Is it possible to work with RC in awk while pipes are used? Or is the problem somewhere else?
I would like to stick to some old portable awk implementation.
awk return-status
add a comment |Â
up vote
2
down vote
favorite
Command not found should produce return code 127:
$ foo; echo $?
bash: foo: command not found...
127
I tried to assign $? to variable rc and then print it, but RC is always 0.
$ foo | awk -v rc="$?" 'BEGINprint rc'
0
bash: foo: command not found...
Found out that it will print correct RC only in this case:
$ qazqaz
bash: qazqaz: command not found...
$ foo | awk -v rc="$?" 'BEGINprint rc'
127
bash: foo: command not found...
Is it possible to work with RC in awk while pipes are used? Or is the problem somewhere else?
I would like to stick to some old portable awk implementation.
awk return-status
Why are you trying to pipe the output of a non-existent command?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 15:43
Because somewhere it is present, somewhere it's not.
â A.D.
Nov 6 '17 at 15:46
Use a semicolon for your awk version, like you did for the first (working ) example
â Jeff Schaller
Nov 6 '17 at 16:12
@val0x00ff I expected something like that. Tried, same result.
â A.D.
Nov 6 '17 at 16:25
@JeffSchaller You are right! But this was just simplified example - I will work with the output in awk.
â A.D.
Nov 6 '17 at 16:26
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Command not found should produce return code 127:
$ foo; echo $?
bash: foo: command not found...
127
I tried to assign $? to variable rc and then print it, but RC is always 0.
$ foo | awk -v rc="$?" 'BEGINprint rc'
0
bash: foo: command not found...
Found out that it will print correct RC only in this case:
$ qazqaz
bash: qazqaz: command not found...
$ foo | awk -v rc="$?" 'BEGINprint rc'
127
bash: foo: command not found...
Is it possible to work with RC in awk while pipes are used? Or is the problem somewhere else?
I would like to stick to some old portable awk implementation.
awk return-status
Command not found should produce return code 127:
$ foo; echo $?
bash: foo: command not found...
127
I tried to assign $? to variable rc and then print it, but RC is always 0.
$ foo | awk -v rc="$?" 'BEGINprint rc'
0
bash: foo: command not found...
Found out that it will print correct RC only in this case:
$ qazqaz
bash: qazqaz: command not found...
$ foo | awk -v rc="$?" 'BEGINprint rc'
127
bash: foo: command not found...
Is it possible to work with RC in awk while pipes are used? Or is the problem somewhere else?
I would like to stick to some old portable awk implementation.
awk return-status
asked Nov 6 '17 at 15:41
A.D.
3851219
3851219
Why are you trying to pipe the output of a non-existent command?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 15:43
Because somewhere it is present, somewhere it's not.
â A.D.
Nov 6 '17 at 15:46
Use a semicolon for your awk version, like you did for the first (working ) example
â Jeff Schaller
Nov 6 '17 at 16:12
@val0x00ff I expected something like that. Tried, same result.
â A.D.
Nov 6 '17 at 16:25
@JeffSchaller You are right! But this was just simplified example - I will work with the output in awk.
â A.D.
Nov 6 '17 at 16:26
add a comment |Â
Why are you trying to pipe the output of a non-existent command?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 15:43
Because somewhere it is present, somewhere it's not.
â A.D.
Nov 6 '17 at 15:46
Use a semicolon for your awk version, like you did for the first (working ) example
â Jeff Schaller
Nov 6 '17 at 16:12
@val0x00ff I expected something like that. Tried, same result.
â A.D.
Nov 6 '17 at 16:25
@JeffSchaller You are right! But this was just simplified example - I will work with the output in awk.
â A.D.
Nov 6 '17 at 16:26
Why are you trying to pipe the output of a non-existent command?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 15:43
Why are you trying to pipe the output of a non-existent command?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 15:43
Because somewhere it is present, somewhere it's not.
â A.D.
Nov 6 '17 at 15:46
Because somewhere it is present, somewhere it's not.
â A.D.
Nov 6 '17 at 15:46
Use a semicolon for your awk version, like you did for the first (working ) example
â Jeff Schaller
Nov 6 '17 at 16:12
Use a semicolon for your awk version, like you did for the first (working ) example
â Jeff Schaller
Nov 6 '17 at 16:12
@val0x00ff I expected something like that. Tried, same result.
â A.D.
Nov 6 '17 at 16:25
@val0x00ff I expected something like that. Tried, same result.
â A.D.
Nov 6 '17 at 16:25
@JeffSchaller You are right! But this was just simplified example - I will work with the output in awk.
â A.D.
Nov 6 '17 at 16:26
@JeffSchaller You are right! But this was just simplified example - I will work with the output in awk.
â A.D.
Nov 6 '17 at 16:26
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
In a pipeline, commands run concurrently. That's the whole point, the output of one is fed to the other in real time.
You only know the exit status of a command when it returns. If you wanted awk to process the output of foo and also get access to its exit status, you'd need to run awk after foo after having stored foo's output somewhere like:
foo > file
awk -v "rc=$?" 'print rc, $0' < file
Alternatively, you could have awk run foo by itself (well, still via a shell to interpret a command line), read its output (through a pipe via its cmd | getline interface to popen()) and get its exit status with:
awk -v cmd=foo '
BEGIN
while ((cmd '
However note that the way awk encodes the exit status varies from one awk implementation to the next. In some it's the status straight as returned by waitpid() or pclose(), in others it's that one divided by 256 (even when foo is killed by a signal)... though you should be able to rely on rc being 0 if and only if the command was successful.
In the case of gawk, it did change recently.
Or you could have the exit status fed at the end through the pipe:
(foo; echo "$?") | awk '
saved = $0
NR > 1
# process the previous line
$0 = prev
print "output:", $0
prev = saved
ENDrc = prev; print rc'
(assuming foo's output ends in a newline character when it's not empty (is valid text)).
Or fed through a separate pipe. For instance on Linux and with a shell other than ksh93:
: extra pipe 4<&0
Seems like the most viable option for me. I just wanted to avoid temp files or running commands within awk exactly because of those compatibility issues.
â A.D.
Nov 6 '17 at 16:32
add a comment |Â
up vote
0
down vote
Use the type command and so:
type test > /dev/null 2>&1
echo $?
0
type fsfsf > /dev/null 2>&1
echo $?
1
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
In a pipeline, commands run concurrently. That's the whole point, the output of one is fed to the other in real time.
You only know the exit status of a command when it returns. If you wanted awk to process the output of foo and also get access to its exit status, you'd need to run awk after foo after having stored foo's output somewhere like:
foo > file
awk -v "rc=$?" 'print rc, $0' < file
Alternatively, you could have awk run foo by itself (well, still via a shell to interpret a command line), read its output (through a pipe via its cmd | getline interface to popen()) and get its exit status with:
awk -v cmd=foo '
BEGIN
while ((cmd '
However note that the way awk encodes the exit status varies from one awk implementation to the next. In some it's the status straight as returned by waitpid() or pclose(), in others it's that one divided by 256 (even when foo is killed by a signal)... though you should be able to rely on rc being 0 if and only if the command was successful.
In the case of gawk, it did change recently.
Or you could have the exit status fed at the end through the pipe:
(foo; echo "$?") | awk '
saved = $0
NR > 1
# process the previous line
$0 = prev
print "output:", $0
prev = saved
ENDrc = prev; print rc'
(assuming foo's output ends in a newline character when it's not empty (is valid text)).
Or fed through a separate pipe. For instance on Linux and with a shell other than ksh93:
: extra pipe 4<&0
Seems like the most viable option for me. I just wanted to avoid temp files or running commands within awk exactly because of those compatibility issues.
â A.D.
Nov 6 '17 at 16:32
add a comment |Â
up vote
4
down vote
accepted
In a pipeline, commands run concurrently. That's the whole point, the output of one is fed to the other in real time.
You only know the exit status of a command when it returns. If you wanted awk to process the output of foo and also get access to its exit status, you'd need to run awk after foo after having stored foo's output somewhere like:
foo > file
awk -v "rc=$?" 'print rc, $0' < file
Alternatively, you could have awk run foo by itself (well, still via a shell to interpret a command line), read its output (through a pipe via its cmd | getline interface to popen()) and get its exit status with:
awk -v cmd=foo '
BEGIN
while ((cmd '
However note that the way awk encodes the exit status varies from one awk implementation to the next. In some it's the status straight as returned by waitpid() or pclose(), in others it's that one divided by 256 (even when foo is killed by a signal)... though you should be able to rely on rc being 0 if and only if the command was successful.
In the case of gawk, it did change recently.
Or you could have the exit status fed at the end through the pipe:
(foo; echo "$?") | awk '
saved = $0
NR > 1
# process the previous line
$0 = prev
print "output:", $0
prev = saved
ENDrc = prev; print rc'
(assuming foo's output ends in a newline character when it's not empty (is valid text)).
Or fed through a separate pipe. For instance on Linux and with a shell other than ksh93:
: extra pipe 4<&0
Seems like the most viable option for me. I just wanted to avoid temp files or running commands within awk exactly because of those compatibility issues.
â A.D.
Nov 6 '17 at 16:32
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
In a pipeline, commands run concurrently. That's the whole point, the output of one is fed to the other in real time.
You only know the exit status of a command when it returns. If you wanted awk to process the output of foo and also get access to its exit status, you'd need to run awk after foo after having stored foo's output somewhere like:
foo > file
awk -v "rc=$?" 'print rc, $0' < file
Alternatively, you could have awk run foo by itself (well, still via a shell to interpret a command line), read its output (through a pipe via its cmd | getline interface to popen()) and get its exit status with:
awk -v cmd=foo '
BEGIN
while ((cmd '
However note that the way awk encodes the exit status varies from one awk implementation to the next. In some it's the status straight as returned by waitpid() or pclose(), in others it's that one divided by 256 (even when foo is killed by a signal)... though you should be able to rely on rc being 0 if and only if the command was successful.
In the case of gawk, it did change recently.
Or you could have the exit status fed at the end through the pipe:
(foo; echo "$?") | awk '
saved = $0
NR > 1
# process the previous line
$0 = prev
print "output:", $0
prev = saved
ENDrc = prev; print rc'
(assuming foo's output ends in a newline character when it's not empty (is valid text)).
Or fed through a separate pipe. For instance on Linux and with a shell other than ksh93:
: extra pipe 4<&0
In a pipeline, commands run concurrently. That's the whole point, the output of one is fed to the other in real time.
You only know the exit status of a command when it returns. If you wanted awk to process the output of foo and also get access to its exit status, you'd need to run awk after foo after having stored foo's output somewhere like:
foo > file
awk -v "rc=$?" 'print rc, $0' < file
Alternatively, you could have awk run foo by itself (well, still via a shell to interpret a command line), read its output (through a pipe via its cmd | getline interface to popen()) and get its exit status with:
awk -v cmd=foo '
BEGIN
while ((cmd '
However note that the way awk encodes the exit status varies from one awk implementation to the next. In some it's the status straight as returned by waitpid() or pclose(), in others it's that one divided by 256 (even when foo is killed by a signal)... though you should be able to rely on rc being 0 if and only if the command was successful.
In the case of gawk, it did change recently.
Or you could have the exit status fed at the end through the pipe:
(foo; echo "$?") | awk '
saved = $0
NR > 1
# process the previous line
$0 = prev
print "output:", $0
prev = saved
ENDrc = prev; print rc'
(assuming foo's output ends in a newline character when it's not empty (is valid text)).
Or fed through a separate pipe. For instance on Linux and with a shell other than ksh93:
: extra pipe 4<&0
edited Nov 6 '17 at 17:29
answered Nov 6 '17 at 16:25
Stéphane Chazelas
283k53521854
283k53521854
Seems like the most viable option for me. I just wanted to avoid temp files or running commands within awk exactly because of those compatibility issues.
â A.D.
Nov 6 '17 at 16:32
add a comment |Â
Seems like the most viable option for me. I just wanted to avoid temp files or running commands within awk exactly because of those compatibility issues.
â A.D.
Nov 6 '17 at 16:32
Seems like the most viable option for me. I just wanted to avoid temp files or running commands within awk exactly because of those compatibility issues.
â A.D.
Nov 6 '17 at 16:32
Seems like the most viable option for me. I just wanted to avoid temp files or running commands within awk exactly because of those compatibility issues.
â A.D.
Nov 6 '17 at 16:32
add a comment |Â
up vote
0
down vote
Use the type command and so:
type test > /dev/null 2>&1
echo $?
0
type fsfsf > /dev/null 2>&1
echo $?
1
add a comment |Â
up vote
0
down vote
Use the type command and so:
type test > /dev/null 2>&1
echo $?
0
type fsfsf > /dev/null 2>&1
echo $?
1
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Use the type command and so:
type test > /dev/null 2>&1
echo $?
0
type fsfsf > /dev/null 2>&1
echo $?
1
Use the type command and so:
type test > /dev/null 2>&1
echo $?
0
type fsfsf > /dev/null 2>&1
echo $?
1
answered Nov 6 '17 at 15:59
Raman Sailopal
1,18117
1,18117
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402854%2faccess-return-code-of-last-command-in-awk%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Why are you trying to pipe the output of a non-existent command?
â Ignacio Vazquez-Abrams
Nov 6 '17 at 15:43
Because somewhere it is present, somewhere it's not.
â A.D.
Nov 6 '17 at 15:46
Use a semicolon for your awk version, like you did for the first (working ) example
â Jeff Schaller
Nov 6 '17 at 16:12
@val0x00ff I expected something like that. Tried, same result.
â A.D.
Nov 6 '17 at 16:25
@JeffSchaller You are right! But this was just simplified example - I will work with the output in awk.
â A.D.
Nov 6 '17 at 16:26