How to treat a command output as text











up vote
5
down vote

favorite












I need to compare a command output with a string.
This is the scenario:



pvs_var=$(pvs | grep "sdb1") 


so pvs var is: /dev/sdb1 vg_name lvm2 a-- 100.00g 0



if [[ $($pvs_var | awk '{ print $2 }') = vg_name ]]; then
do something
fi


The issue is that the output of the if statement is



-bash: /dev/sdb1: Permission denied


I don't understand this behavior.
Thank you










share|improve this question


























    up vote
    5
    down vote

    favorite












    I need to compare a command output with a string.
    This is the scenario:



    pvs_var=$(pvs | grep "sdb1") 


    so pvs var is: /dev/sdb1 vg_name lvm2 a-- 100.00g 0



    if [[ $($pvs_var | awk '{ print $2 }') = vg_name ]]; then
    do something
    fi


    The issue is that the output of the if statement is



    -bash: /dev/sdb1: Permission denied


    I don't understand this behavior.
    Thank you










    share|improve this question
























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I need to compare a command output with a string.
      This is the scenario:



      pvs_var=$(pvs | grep "sdb1") 


      so pvs var is: /dev/sdb1 vg_name lvm2 a-- 100.00g 0



      if [[ $($pvs_var | awk '{ print $2 }') = vg_name ]]; then
      do something
      fi


      The issue is that the output of the if statement is



      -bash: /dev/sdb1: Permission denied


      I don't understand this behavior.
      Thank you










      share|improve this question













      I need to compare a command output with a string.
      This is the scenario:



      pvs_var=$(pvs | grep "sdb1") 


      so pvs var is: /dev/sdb1 vg_name lvm2 a-- 100.00g 0



      if [[ $($pvs_var | awk '{ print $2 }') = vg_name ]]; then
      do something
      fi


      The issue is that the output of the if statement is



      -bash: /dev/sdb1: Permission denied


      I don't understand this behavior.
      Thank you







      bash command string output






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 30 at 15:42









      intore

      10619




      10619






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          12
          down vote



          accepted










          You are attempting to execute the contents of $pvs_var as a command, rather than passing the string to awk.



          To fix this, add an echo or printf in your if statement:



          if [[ $(echo "$pvs_var" | awk '{ print $2 }') = vg_name ]]; then
          do something
          fi





          share|improve this answer























          • Since the shell is bash, might as well awk '{ print $2 }' <<<"$pvs_var"
            – D. Ben Knoble
            Nov 30 at 23:13










          • @D.BenKnoble: Even better, we can replace the double brackets with single brackets and then it’s POSIX compliant.
            – Peschke
            Nov 30 at 23:22










          • @Peschke With single brackets, you'd want to add double-quotes around the $( ) expression.
            – Gordon Davisson
            Dec 1 at 3:49










          • You definitely want printf in the general case. Echo can do strange things if the variable contains backslashes or starts with a dash.
            – Kevin
            Dec 1 at 17:08


















          up vote
          4
          down vote













          Get the output in JSON format, and then you'll be able to extract information in a more reliable way:



          pv_info=$(pvs -o pv_all,vg_all --unit b --nosuffix --reportformat json)
          sdb1_vg=$(
          printf '%sn' "$pv_info" |
          jq -r '.report.pv|select(.pv_name == "/dev/sdb1").vg_name'
          )

          if [ "$sdb1_vg" = vg_name ]; then...


          Or use a proper programming language with a JSON library instead of a shell (ksh93 does have JSON support though in its upcoming version).



          If it's just that one query you want to do, pvs can also do all the work for you:



          sdb1_vg=$(
          pvs -o vg_name -S pv_name=/dev/sdb1 --no-heading --config 'log{prefix=""}'
          )


          Also remember to quote your variables and avoid echo.






          share|improve this answer




























            up vote
            2
            down vote













            If you want to compare the VG from the output, then it might be easier to pre-process that:



            # if you still need it
            pvs_var=$(pvs | grep "sdb1")
            vg_name=$(pvs | grep "sdb1" | awk '{print $2}')
            if [ "$vg_name" = "vg_name" ]; then
            echo do something
            fi


            What you were doing with




            $($pvs_var | awk '{ print $2 }')




            was initiating a command substitution $( ... ) whose first command was $pvs_var. Bash dutifully substituted the value of the variable and then attempted to execute it. That's not what you wanted.



            Another alternative would be to send the variable as a here-string to the awk command:



            # ...
            if [ $(awk '{print $2}' <<< "$pvs_var") = "vg_name" ]; then
            # ...


            Here, the command substitution is calling awk and passing it input on stdin -- the contents of the $pvs_var variable.






            share|improve this answer




























              up vote
              0
              down vote













              You can try only with awk :



              pvs | awk -v search='vg_name' '/sdb1/&&$2==search{exit 1}' || echo "ok"





              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: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                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%2f485181%2fhow-to-treat-a-command-output-as-text%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                12
                down vote



                accepted










                You are attempting to execute the contents of $pvs_var as a command, rather than passing the string to awk.



                To fix this, add an echo or printf in your if statement:



                if [[ $(echo "$pvs_var" | awk '{ print $2 }') = vg_name ]]; then
                do something
                fi





                share|improve this answer























                • Since the shell is bash, might as well awk '{ print $2 }' <<<"$pvs_var"
                  – D. Ben Knoble
                  Nov 30 at 23:13










                • @D.BenKnoble: Even better, we can replace the double brackets with single brackets and then it’s POSIX compliant.
                  – Peschke
                  Nov 30 at 23:22










                • @Peschke With single brackets, you'd want to add double-quotes around the $( ) expression.
                  – Gordon Davisson
                  Dec 1 at 3:49










                • You definitely want printf in the general case. Echo can do strange things if the variable contains backslashes or starts with a dash.
                  – Kevin
                  Dec 1 at 17:08















                up vote
                12
                down vote



                accepted










                You are attempting to execute the contents of $pvs_var as a command, rather than passing the string to awk.



                To fix this, add an echo or printf in your if statement:



                if [[ $(echo "$pvs_var" | awk '{ print $2 }') = vg_name ]]; then
                do something
                fi





                share|improve this answer























                • Since the shell is bash, might as well awk '{ print $2 }' <<<"$pvs_var"
                  – D. Ben Knoble
                  Nov 30 at 23:13










                • @D.BenKnoble: Even better, we can replace the double brackets with single brackets and then it’s POSIX compliant.
                  – Peschke
                  Nov 30 at 23:22










                • @Peschke With single brackets, you'd want to add double-quotes around the $( ) expression.
                  – Gordon Davisson
                  Dec 1 at 3:49










                • You definitely want printf in the general case. Echo can do strange things if the variable contains backslashes or starts with a dash.
                  – Kevin
                  Dec 1 at 17:08













                up vote
                12
                down vote



                accepted







                up vote
                12
                down vote



                accepted






                You are attempting to execute the contents of $pvs_var as a command, rather than passing the string to awk.



                To fix this, add an echo or printf in your if statement:



                if [[ $(echo "$pvs_var" | awk '{ print $2 }') = vg_name ]]; then
                do something
                fi





                share|improve this answer














                You are attempting to execute the contents of $pvs_var as a command, rather than passing the string to awk.



                To fix this, add an echo or printf in your if statement:



                if [[ $(echo "$pvs_var" | awk '{ print $2 }') = vg_name ]]; then
                do something
                fi






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 1 at 0:50

























                answered Nov 30 at 15:58









                Peschke

                2,400924




                2,400924












                • Since the shell is bash, might as well awk '{ print $2 }' <<<"$pvs_var"
                  – D. Ben Knoble
                  Nov 30 at 23:13










                • @D.BenKnoble: Even better, we can replace the double brackets with single brackets and then it’s POSIX compliant.
                  – Peschke
                  Nov 30 at 23:22










                • @Peschke With single brackets, you'd want to add double-quotes around the $( ) expression.
                  – Gordon Davisson
                  Dec 1 at 3:49










                • You definitely want printf in the general case. Echo can do strange things if the variable contains backslashes or starts with a dash.
                  – Kevin
                  Dec 1 at 17:08


















                • Since the shell is bash, might as well awk '{ print $2 }' <<<"$pvs_var"
                  – D. Ben Knoble
                  Nov 30 at 23:13










                • @D.BenKnoble: Even better, we can replace the double brackets with single brackets and then it’s POSIX compliant.
                  – Peschke
                  Nov 30 at 23:22










                • @Peschke With single brackets, you'd want to add double-quotes around the $( ) expression.
                  – Gordon Davisson
                  Dec 1 at 3:49










                • You definitely want printf in the general case. Echo can do strange things if the variable contains backslashes or starts with a dash.
                  – Kevin
                  Dec 1 at 17:08
















                Since the shell is bash, might as well awk '{ print $2 }' <<<"$pvs_var"
                – D. Ben Knoble
                Nov 30 at 23:13




                Since the shell is bash, might as well awk '{ print $2 }' <<<"$pvs_var"
                – D. Ben Knoble
                Nov 30 at 23:13












                @D.BenKnoble: Even better, we can replace the double brackets with single brackets and then it’s POSIX compliant.
                – Peschke
                Nov 30 at 23:22




                @D.BenKnoble: Even better, we can replace the double brackets with single brackets and then it’s POSIX compliant.
                – Peschke
                Nov 30 at 23:22












                @Peschke With single brackets, you'd want to add double-quotes around the $( ) expression.
                – Gordon Davisson
                Dec 1 at 3:49




                @Peschke With single brackets, you'd want to add double-quotes around the $( ) expression.
                – Gordon Davisson
                Dec 1 at 3:49












                You definitely want printf in the general case. Echo can do strange things if the variable contains backslashes or starts with a dash.
                – Kevin
                Dec 1 at 17:08




                You definitely want printf in the general case. Echo can do strange things if the variable contains backslashes or starts with a dash.
                – Kevin
                Dec 1 at 17:08












                up vote
                4
                down vote













                Get the output in JSON format, and then you'll be able to extract information in a more reliable way:



                pv_info=$(pvs -o pv_all,vg_all --unit b --nosuffix --reportformat json)
                sdb1_vg=$(
                printf '%sn' "$pv_info" |
                jq -r '.report.pv|select(.pv_name == "/dev/sdb1").vg_name'
                )

                if [ "$sdb1_vg" = vg_name ]; then...


                Or use a proper programming language with a JSON library instead of a shell (ksh93 does have JSON support though in its upcoming version).



                If it's just that one query you want to do, pvs can also do all the work for you:



                sdb1_vg=$(
                pvs -o vg_name -S pv_name=/dev/sdb1 --no-heading --config 'log{prefix=""}'
                )


                Also remember to quote your variables and avoid echo.






                share|improve this answer

























                  up vote
                  4
                  down vote













                  Get the output in JSON format, and then you'll be able to extract information in a more reliable way:



                  pv_info=$(pvs -o pv_all,vg_all --unit b --nosuffix --reportformat json)
                  sdb1_vg=$(
                  printf '%sn' "$pv_info" |
                  jq -r '.report.pv|select(.pv_name == "/dev/sdb1").vg_name'
                  )

                  if [ "$sdb1_vg" = vg_name ]; then...


                  Or use a proper programming language with a JSON library instead of a shell (ksh93 does have JSON support though in its upcoming version).



                  If it's just that one query you want to do, pvs can also do all the work for you:



                  sdb1_vg=$(
                  pvs -o vg_name -S pv_name=/dev/sdb1 --no-heading --config 'log{prefix=""}'
                  )


                  Also remember to quote your variables and avoid echo.






                  share|improve this answer























                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    Get the output in JSON format, and then you'll be able to extract information in a more reliable way:



                    pv_info=$(pvs -o pv_all,vg_all --unit b --nosuffix --reportformat json)
                    sdb1_vg=$(
                    printf '%sn' "$pv_info" |
                    jq -r '.report.pv|select(.pv_name == "/dev/sdb1").vg_name'
                    )

                    if [ "$sdb1_vg" = vg_name ]; then...


                    Or use a proper programming language with a JSON library instead of a shell (ksh93 does have JSON support though in its upcoming version).



                    If it's just that one query you want to do, pvs can also do all the work for you:



                    sdb1_vg=$(
                    pvs -o vg_name -S pv_name=/dev/sdb1 --no-heading --config 'log{prefix=""}'
                    )


                    Also remember to quote your variables and avoid echo.






                    share|improve this answer












                    Get the output in JSON format, and then you'll be able to extract information in a more reliable way:



                    pv_info=$(pvs -o pv_all,vg_all --unit b --nosuffix --reportformat json)
                    sdb1_vg=$(
                    printf '%sn' "$pv_info" |
                    jq -r '.report.pv|select(.pv_name == "/dev/sdb1").vg_name'
                    )

                    if [ "$sdb1_vg" = vg_name ]; then...


                    Or use a proper programming language with a JSON library instead of a shell (ksh93 does have JSON support though in its upcoming version).



                    If it's just that one query you want to do, pvs can also do all the work for you:



                    sdb1_vg=$(
                    pvs -o vg_name -S pv_name=/dev/sdb1 --no-heading --config 'log{prefix=""}'
                    )


                    Also remember to quote your variables and avoid echo.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 30 at 17:40









                    Stéphane Chazelas

                    296k54560905




                    296k54560905






















                        up vote
                        2
                        down vote













                        If you want to compare the VG from the output, then it might be easier to pre-process that:



                        # if you still need it
                        pvs_var=$(pvs | grep "sdb1")
                        vg_name=$(pvs | grep "sdb1" | awk '{print $2}')
                        if [ "$vg_name" = "vg_name" ]; then
                        echo do something
                        fi


                        What you were doing with




                        $($pvs_var | awk '{ print $2 }')




                        was initiating a command substitution $( ... ) whose first command was $pvs_var. Bash dutifully substituted the value of the variable and then attempted to execute it. That's not what you wanted.



                        Another alternative would be to send the variable as a here-string to the awk command:



                        # ...
                        if [ $(awk '{print $2}' <<< "$pvs_var") = "vg_name" ]; then
                        # ...


                        Here, the command substitution is calling awk and passing it input on stdin -- the contents of the $pvs_var variable.






                        share|improve this answer

























                          up vote
                          2
                          down vote













                          If you want to compare the VG from the output, then it might be easier to pre-process that:



                          # if you still need it
                          pvs_var=$(pvs | grep "sdb1")
                          vg_name=$(pvs | grep "sdb1" | awk '{print $2}')
                          if [ "$vg_name" = "vg_name" ]; then
                          echo do something
                          fi


                          What you were doing with




                          $($pvs_var | awk '{ print $2 }')




                          was initiating a command substitution $( ... ) whose first command was $pvs_var. Bash dutifully substituted the value of the variable and then attempted to execute it. That's not what you wanted.



                          Another alternative would be to send the variable as a here-string to the awk command:



                          # ...
                          if [ $(awk '{print $2}' <<< "$pvs_var") = "vg_name" ]; then
                          # ...


                          Here, the command substitution is calling awk and passing it input on stdin -- the contents of the $pvs_var variable.






                          share|improve this answer























                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            If you want to compare the VG from the output, then it might be easier to pre-process that:



                            # if you still need it
                            pvs_var=$(pvs | grep "sdb1")
                            vg_name=$(pvs | grep "sdb1" | awk '{print $2}')
                            if [ "$vg_name" = "vg_name" ]; then
                            echo do something
                            fi


                            What you were doing with




                            $($pvs_var | awk '{ print $2 }')




                            was initiating a command substitution $( ... ) whose first command was $pvs_var. Bash dutifully substituted the value of the variable and then attempted to execute it. That's not what you wanted.



                            Another alternative would be to send the variable as a here-string to the awk command:



                            # ...
                            if [ $(awk '{print $2}' <<< "$pvs_var") = "vg_name" ]; then
                            # ...


                            Here, the command substitution is calling awk and passing it input on stdin -- the contents of the $pvs_var variable.






                            share|improve this answer












                            If you want to compare the VG from the output, then it might be easier to pre-process that:



                            # if you still need it
                            pvs_var=$(pvs | grep "sdb1")
                            vg_name=$(pvs | grep "sdb1" | awk '{print $2}')
                            if [ "$vg_name" = "vg_name" ]; then
                            echo do something
                            fi


                            What you were doing with




                            $($pvs_var | awk '{ print $2 }')




                            was initiating a command substitution $( ... ) whose first command was $pvs_var. Bash dutifully substituted the value of the variable and then attempted to execute it. That's not what you wanted.



                            Another alternative would be to send the variable as a here-string to the awk command:



                            # ...
                            if [ $(awk '{print $2}' <<< "$pvs_var") = "vg_name" ]; then
                            # ...


                            Here, the command substitution is calling awk and passing it input on stdin -- the contents of the $pvs_var variable.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 30 at 15:55









                            Jeff Schaller

                            37.4k1052121




                            37.4k1052121






















                                up vote
                                0
                                down vote













                                You can try only with awk :



                                pvs | awk -v search='vg_name' '/sdb1/&&$2==search{exit 1}' || echo "ok"





                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  You can try only with awk :



                                  pvs | awk -v search='vg_name' '/sdb1/&&$2==search{exit 1}' || echo "ok"





                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    You can try only with awk :



                                    pvs | awk -v search='vg_name' '/sdb1/&&$2==search{exit 1}' || echo "ok"





                                    share|improve this answer












                                    You can try only with awk :



                                    pvs | awk -v search='vg_name' '/sdb1/&&$2==search{exit 1}' || echo "ok"






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 30 at 16:23









                                    ctac_

                                    1,344117




                                    1,344117






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485181%2fhow-to-treat-a-command-output-as-text%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                        Aardman Animations

                                        Are they similar matrix