Awk detect stdin

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Perl, Ruby and PHP can detect stdin:



$ perl -e 'print -t ? "no stdin" : "stdin"'
no stdin

$ echo | perl -e 'print -t ? "no stdin" : "stdin"'
stdin

$ ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
no stdin

$ echo | ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
stdin

$ php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
no stdin

$ echo | php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
stdin


Does Awk have some way to do this? I tried ARGC but its the same in both cases:



$ awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
no stdin

$ echo | awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
no stdin






share|improve this question
























    up vote
    0
    down vote

    favorite












    Perl, Ruby and PHP can detect stdin:



    $ perl -e 'print -t ? "no stdin" : "stdin"'
    no stdin

    $ echo | perl -e 'print -t ? "no stdin" : "stdin"'
    stdin

    $ ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
    no stdin

    $ echo | ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
    stdin

    $ php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
    no stdin

    $ echo | php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
    stdin


    Does Awk have some way to do this? I tried ARGC but its the same in both cases:



    $ awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
    no stdin

    $ echo | awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
    no stdin






    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Perl, Ruby and PHP can detect stdin:



      $ perl -e 'print -t ? "no stdin" : "stdin"'
      no stdin

      $ echo | perl -e 'print -t ? "no stdin" : "stdin"'
      stdin

      $ ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
      no stdin

      $ echo | ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
      stdin

      $ php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
      no stdin

      $ echo | php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
      stdin


      Does Awk have some way to do this? I tried ARGC but its the same in both cases:



      $ awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
      no stdin

      $ echo | awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
      no stdin






      share|improve this question












      Perl, Ruby and PHP can detect stdin:



      $ perl -e 'print -t ? "no stdin" : "stdin"'
      no stdin

      $ echo | perl -e 'print -t ? "no stdin" : "stdin"'
      stdin

      $ ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
      no stdin

      $ echo | ruby -e 'puts $stdin.isatty ? "no stdin" : "stdin"'
      stdin

      $ php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
      no stdin

      $ echo | php -r 'print posix_isatty(STDIN) ? "no stdin" : "stdin";'
      stdin


      Does Awk have some way to do this? I tried ARGC but its the same in both cases:



      $ awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
      no stdin

      $ echo | awk 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"'
      no stdin








      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 31 at 14:22









      Steven Penny

      2,28821635




      2,28821635




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Your perl, ruby and php examples test whether stdin exists. Your awk approach just tests whether stdin should be used.



          You can use system("tty") in awk to do that.






          share|improve this answer
















          • 1




            Nice - looks like system("[ -t 0 ]") works too
            – Steven Penny
            Mar 31 at 14:52

















          up vote
          1
          down vote














          ARGC is the number of command-line arguments present. (indexed from 1)




          In both mentioned cases the awk command/expression 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"' is considered as the 1st argument independently of whether any data was piped to the current command.



          To check for the file name of the terminal connected to standard input:



          echo "a" | awk 'BEGIN "tty" '
          stdin

          awk 'BEGIN "tty" '
          no stdin





          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%2f434678%2fawk-detect-stdin%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
            3
            down vote



            accepted










            Your perl, ruby and php examples test whether stdin exists. Your awk approach just tests whether stdin should be used.



            You can use system("tty") in awk to do that.






            share|improve this answer
















            • 1




              Nice - looks like system("[ -t 0 ]") works too
              – Steven Penny
              Mar 31 at 14:52














            up vote
            3
            down vote



            accepted










            Your perl, ruby and php examples test whether stdin exists. Your awk approach just tests whether stdin should be used.



            You can use system("tty") in awk to do that.






            share|improve this answer
















            • 1




              Nice - looks like system("[ -t 0 ]") works too
              – Steven Penny
              Mar 31 at 14:52












            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            Your perl, ruby and php examples test whether stdin exists. Your awk approach just tests whether stdin should be used.



            You can use system("tty") in awk to do that.






            share|improve this answer












            Your perl, ruby and php examples test whether stdin exists. Your awk approach just tests whether stdin should be used.



            You can use system("tty") in awk to do that.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 31 at 14:48









            Hauke Laging

            53.2k1282130




            53.2k1282130







            • 1




              Nice - looks like system("[ -t 0 ]") works too
              – Steven Penny
              Mar 31 at 14:52












            • 1




              Nice - looks like system("[ -t 0 ]") works too
              – Steven Penny
              Mar 31 at 14:52







            1




            1




            Nice - looks like system("[ -t 0 ]") works too
            – Steven Penny
            Mar 31 at 14:52




            Nice - looks like system("[ -t 0 ]") works too
            – Steven Penny
            Mar 31 at 14:52












            up vote
            1
            down vote














            ARGC is the number of command-line arguments present. (indexed from 1)




            In both mentioned cases the awk command/expression 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"' is considered as the 1st argument independently of whether any data was piped to the current command.



            To check for the file name of the terminal connected to standard input:



            echo "a" | awk 'BEGIN "tty" '
            stdin

            awk 'BEGIN "tty" '
            no stdin





            share|improve this answer


























              up vote
              1
              down vote














              ARGC is the number of command-line arguments present. (indexed from 1)




              In both mentioned cases the awk command/expression 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"' is considered as the 1st argument independently of whether any data was piped to the current command.



              To check for the file name of the terminal connected to standard input:



              echo "a" | awk 'BEGIN "tty" '
              stdin

              awk 'BEGIN "tty" '
              no stdin





              share|improve this answer
























                up vote
                1
                down vote










                up vote
                1
                down vote










                ARGC is the number of command-line arguments present. (indexed from 1)




                In both mentioned cases the awk command/expression 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"' is considered as the 1st argument independently of whether any data was piped to the current command.



                To check for the file name of the terminal connected to standard input:



                echo "a" | awk 'BEGIN "tty" '
                stdin

                awk 'BEGIN "tty" '
                no stdin





                share|improve this answer















                ARGC is the number of command-line arguments present. (indexed from 1)




                In both mentioned cases the awk command/expression 'BEGIN print ARGC == 1 ? "no stdin" : "stdin"' is considered as the 1st argument independently of whether any data was piped to the current command.



                To check for the file name of the terminal connected to standard input:



                echo "a" | awk 'BEGIN "tty" '
                stdin

                awk 'BEGIN "tty" '
                no stdin






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 31 at 15:02

























                answered Mar 31 at 14:46









                RomanPerekhrest

                22.4k12144




                22.4k12144






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434678%2fawk-detect-stdin%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay