Access return code of last command in AWK

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question




















  • 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














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.







share|improve this question




















  • 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












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.







share|improve this question












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.









share|improve this question











share|improve this question




share|improve this question










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
















  • 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










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





share|improve this answer






















  • 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

















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





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer






















    • 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














    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





    share|improve this answer






















    • 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












    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





    share|improve this answer














    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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












    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





    share|improve this answer
























      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





      share|improve this answer






















        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





        share|improve this answer












        Use the type command and so:



        type test > /dev/null 2>&1
        echo $?
        0

        type fsfsf > /dev/null 2>&1
        echo $?
        1






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 6 '17 at 15:59









        Raman Sailopal

        1,18117




        1,18117



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)