How to compare “abc” with abc in a shell script?

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











up vote
-3
down vote

favorite












My code need compare "stop" with stop this is stand bash string.



pi@raspberrypi:~/Voice $ ./test.sh | more
"stop"
stop


My code:



#!/bin/bash
command=stop
while :
do
QUESTION=$(cat stt.txt) #stt,txt has command "stop"
echo $QUESTION
echo $command
if [ "$QUESTION" == "$command" ]; then
echo "You said "stop"!"
break
fi
done


I had try different command="stop", the result is same.
I try to put command=$('stop'), it's okay only one time, then it complains:
./test.sh: line 2: stop: command not found.



I don't know why it is suddenly stop working to set stop as command, not "stop"










share|improve this question























  • "the result is same" — what is the result? You didn't actually say. What did you expect to have happen?
    – Wildcard
    Jun 25 '16 at 0:59







  • 1




    It doesn't answer your question, but you should definitely read: Why does my shell script choke on whitespace or other special characters?
    – Wildcard
    Jun 25 '16 at 1:01










  • The result is $QUESTIONS is "stop", $command is stop, It cannot compare with eachother
    – rfid gao
    Jun 25 '16 at 3:01










  • the string in stt,txt is "stop", this is output from other application, I cannot change it, so echo $QUESTION is "stop", any string I set in command it will out put as stop.
    – rfid gao
    Jun 25 '16 at 3:05














up vote
-3
down vote

favorite












My code need compare "stop" with stop this is stand bash string.



pi@raspberrypi:~/Voice $ ./test.sh | more
"stop"
stop


My code:



#!/bin/bash
command=stop
while :
do
QUESTION=$(cat stt.txt) #stt,txt has command "stop"
echo $QUESTION
echo $command
if [ "$QUESTION" == "$command" ]; then
echo "You said "stop"!"
break
fi
done


I had try different command="stop", the result is same.
I try to put command=$('stop'), it's okay only one time, then it complains:
./test.sh: line 2: stop: command not found.



I don't know why it is suddenly stop working to set stop as command, not "stop"










share|improve this question























  • "the result is same" — what is the result? You didn't actually say. What did you expect to have happen?
    – Wildcard
    Jun 25 '16 at 0:59







  • 1




    It doesn't answer your question, but you should definitely read: Why does my shell script choke on whitespace or other special characters?
    – Wildcard
    Jun 25 '16 at 1:01










  • The result is $QUESTIONS is "stop", $command is stop, It cannot compare with eachother
    – rfid gao
    Jun 25 '16 at 3:01










  • the string in stt,txt is "stop", this is output from other application, I cannot change it, so echo $QUESTION is "stop", any string I set in command it will out put as stop.
    – rfid gao
    Jun 25 '16 at 3:05












up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











My code need compare "stop" with stop this is stand bash string.



pi@raspberrypi:~/Voice $ ./test.sh | more
"stop"
stop


My code:



#!/bin/bash
command=stop
while :
do
QUESTION=$(cat stt.txt) #stt,txt has command "stop"
echo $QUESTION
echo $command
if [ "$QUESTION" == "$command" ]; then
echo "You said "stop"!"
break
fi
done


I had try different command="stop", the result is same.
I try to put command=$('stop'), it's okay only one time, then it complains:
./test.sh: line 2: stop: command not found.



I don't know why it is suddenly stop working to set stop as command, not "stop"










share|improve this question















My code need compare "stop" with stop this is stand bash string.



pi@raspberrypi:~/Voice $ ./test.sh | more
"stop"
stop


My code:



#!/bin/bash
command=stop
while :
do
QUESTION=$(cat stt.txt) #stt,txt has command "stop"
echo $QUESTION
echo $command
if [ "$QUESTION" == "$command" ]; then
echo "You said "stop"!"
break
fi
done


I had try different command="stop", the result is same.
I try to put command=$('stop'), it's okay only one time, then it complains:
./test.sh: line 2: stop: command not found.



I don't know why it is suddenly stop working to set stop as command, not "stop"







shell-script variable test






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:34









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Jun 25 '16 at 0:44









rfid gao

191




191











  • "the result is same" — what is the result? You didn't actually say. What did you expect to have happen?
    – Wildcard
    Jun 25 '16 at 0:59







  • 1




    It doesn't answer your question, but you should definitely read: Why does my shell script choke on whitespace or other special characters?
    – Wildcard
    Jun 25 '16 at 1:01










  • The result is $QUESTIONS is "stop", $command is stop, It cannot compare with eachother
    – rfid gao
    Jun 25 '16 at 3:01










  • the string in stt,txt is "stop", this is output from other application, I cannot change it, so echo $QUESTION is "stop", any string I set in command it will out put as stop.
    – rfid gao
    Jun 25 '16 at 3:05
















  • "the result is same" — what is the result? You didn't actually say. What did you expect to have happen?
    – Wildcard
    Jun 25 '16 at 0:59







  • 1




    It doesn't answer your question, but you should definitely read: Why does my shell script choke on whitespace or other special characters?
    – Wildcard
    Jun 25 '16 at 1:01










  • The result is $QUESTIONS is "stop", $command is stop, It cannot compare with eachother
    – rfid gao
    Jun 25 '16 at 3:01










  • the string in stt,txt is "stop", this is output from other application, I cannot change it, so echo $QUESTION is "stop", any string I set in command it will out put as stop.
    – rfid gao
    Jun 25 '16 at 3:05















"the result is same" — what is the result? You didn't actually say. What did you expect to have happen?
– Wildcard
Jun 25 '16 at 0:59





"the result is same" — what is the result? You didn't actually say. What did you expect to have happen?
– Wildcard
Jun 25 '16 at 0:59





1




1




It doesn't answer your question, but you should definitely read: Why does my shell script choke on whitespace or other special characters?
– Wildcard
Jun 25 '16 at 1:01




It doesn't answer your question, but you should definitely read: Why does my shell script choke on whitespace or other special characters?
– Wildcard
Jun 25 '16 at 1:01












The result is $QUESTIONS is "stop", $command is stop, It cannot compare with eachother
– rfid gao
Jun 25 '16 at 3:01




The result is $QUESTIONS is "stop", $command is stop, It cannot compare with eachother
– rfid gao
Jun 25 '16 at 3:01












the string in stt,txt is "stop", this is output from other application, I cannot change it, so echo $QUESTION is "stop", any string I set in command it will out put as stop.
– rfid gao
Jun 25 '16 at 3:05




the string in stt,txt is "stop", this is output from other application, I cannot change it, so echo $QUESTION is "stop", any string I set in command it will out put as stop.
– rfid gao
Jun 25 '16 at 3:05










3 Answers
3






active

oldest

votes

















up vote
2
down vote













Thank everybody's help.
I try different one, this is working for me!



#!/bin/bash
command=""stop""
while :
do
QUESTION=$(cat stt.txt) #stt,txt has command "stop"
echo $QUESTION
echo $command
if [ "$QUESTION" = "$command" ]; then
echo "You said $command"!
break
fi
done





share|improve this answer





























    up vote
    1
    down vote













    #!/bin/bash
    command="stop"
    while :
    do
    QUESTION=$(cat stt.txt) #stt,txt has command "stop"
    echo $QUESTION
    echo $command
    if [ "$QUESTION" == "$command" ]; then
    echo "You said $command"!
    break
    fi
    done


    I made a two changes to your script.




    1. All strings entered directly into scripts for use in variables should be quoted, otherwise bash will try to interpret them as commands. As such this is not a valid way to declare a variable 'command' with a string value 'stop'.



      command=stop


      This is a valid way.



      command="stop"



    2. Also bash will try to interpret your ! as you trying to recall an event, you would need to place that outside your quotes and escape it.



      echo "You said $command"!






    share|improve this answer




















    • Thank all the people's help here. I had try Zachary's code. The issue is same. $QUESTION is "stop", $command is stop, so they cannot be equal. I cannot get my result: Cpi@raspberrypi:~/Voice $ ./test.sh | more. I get "stop" stop.
      – rfid gao
      Jun 25 '16 at 2:57


















    up vote
    1
    down vote













    grep -q '"stop"' < in > /dev/null && echo hooray





    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%2f291990%2fhow-to-compare-abc-with-abc-in-a-shell-script%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      Thank everybody's help.
      I try different one, this is working for me!



      #!/bin/bash
      command=""stop""
      while :
      do
      QUESTION=$(cat stt.txt) #stt,txt has command "stop"
      echo $QUESTION
      echo $command
      if [ "$QUESTION" = "$command" ]; then
      echo "You said $command"!
      break
      fi
      done





      share|improve this answer


























        up vote
        2
        down vote













        Thank everybody's help.
        I try different one, this is working for me!



        #!/bin/bash
        command=""stop""
        while :
        do
        QUESTION=$(cat stt.txt) #stt,txt has command "stop"
        echo $QUESTION
        echo $command
        if [ "$QUESTION" = "$command" ]; then
        echo "You said $command"!
        break
        fi
        done





        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          Thank everybody's help.
          I try different one, this is working for me!



          #!/bin/bash
          command=""stop""
          while :
          do
          QUESTION=$(cat stt.txt) #stt,txt has command "stop"
          echo $QUESTION
          echo $command
          if [ "$QUESTION" = "$command" ]; then
          echo "You said $command"!
          break
          fi
          done





          share|improve this answer














          Thank everybody's help.
          I try different one, this is working for me!



          #!/bin/bash
          command=""stop""
          while :
          do
          QUESTION=$(cat stt.txt) #stt,txt has command "stop"
          echo $QUESTION
          echo $command
          if [ "$QUESTION" = "$command" ]; then
          echo "You said $command"!
          break
          fi
          done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 25 '16 at 3:35









          Mongrel

          2,04131345




          2,04131345










          answered Jun 25 '16 at 3:18









          rfid gao

          191




          191






















              up vote
              1
              down vote













              #!/bin/bash
              command="stop"
              while :
              do
              QUESTION=$(cat stt.txt) #stt,txt has command "stop"
              echo $QUESTION
              echo $command
              if [ "$QUESTION" == "$command" ]; then
              echo "You said $command"!
              break
              fi
              done


              I made a two changes to your script.




              1. All strings entered directly into scripts for use in variables should be quoted, otherwise bash will try to interpret them as commands. As such this is not a valid way to declare a variable 'command' with a string value 'stop'.



                command=stop


                This is a valid way.



                command="stop"



              2. Also bash will try to interpret your ! as you trying to recall an event, you would need to place that outside your quotes and escape it.



                echo "You said $command"!






              share|improve this answer




















              • Thank all the people's help here. I had try Zachary's code. The issue is same. $QUESTION is "stop", $command is stop, so they cannot be equal. I cannot get my result: Cpi@raspberrypi:~/Voice $ ./test.sh | more. I get "stop" stop.
                – rfid gao
                Jun 25 '16 at 2:57















              up vote
              1
              down vote













              #!/bin/bash
              command="stop"
              while :
              do
              QUESTION=$(cat stt.txt) #stt,txt has command "stop"
              echo $QUESTION
              echo $command
              if [ "$QUESTION" == "$command" ]; then
              echo "You said $command"!
              break
              fi
              done


              I made a two changes to your script.




              1. All strings entered directly into scripts for use in variables should be quoted, otherwise bash will try to interpret them as commands. As such this is not a valid way to declare a variable 'command' with a string value 'stop'.



                command=stop


                This is a valid way.



                command="stop"



              2. Also bash will try to interpret your ! as you trying to recall an event, you would need to place that outside your quotes and escape it.



                echo "You said $command"!






              share|improve this answer




















              • Thank all the people's help here. I had try Zachary's code. The issue is same. $QUESTION is "stop", $command is stop, so they cannot be equal. I cannot get my result: Cpi@raspberrypi:~/Voice $ ./test.sh | more. I get "stop" stop.
                – rfid gao
                Jun 25 '16 at 2:57













              up vote
              1
              down vote










              up vote
              1
              down vote









              #!/bin/bash
              command="stop"
              while :
              do
              QUESTION=$(cat stt.txt) #stt,txt has command "stop"
              echo $QUESTION
              echo $command
              if [ "$QUESTION" == "$command" ]; then
              echo "You said $command"!
              break
              fi
              done


              I made a two changes to your script.




              1. All strings entered directly into scripts for use in variables should be quoted, otherwise bash will try to interpret them as commands. As such this is not a valid way to declare a variable 'command' with a string value 'stop'.



                command=stop


                This is a valid way.



                command="stop"



              2. Also bash will try to interpret your ! as you trying to recall an event, you would need to place that outside your quotes and escape it.



                echo "You said $command"!






              share|improve this answer












              #!/bin/bash
              command="stop"
              while :
              do
              QUESTION=$(cat stt.txt) #stt,txt has command "stop"
              echo $QUESTION
              echo $command
              if [ "$QUESTION" == "$command" ]; then
              echo "You said $command"!
              break
              fi
              done


              I made a two changes to your script.




              1. All strings entered directly into scripts for use in variables should be quoted, otherwise bash will try to interpret them as commands. As such this is not a valid way to declare a variable 'command' with a string value 'stop'.



                command=stop


                This is a valid way.



                command="stop"



              2. Also bash will try to interpret your ! as you trying to recall an event, you would need to place that outside your quotes and escape it.



                echo "You said $command"!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 25 '16 at 2:42









              Zachary Brady

              3,386831




              3,386831











              • Thank all the people's help here. I had try Zachary's code. The issue is same. $QUESTION is "stop", $command is stop, so they cannot be equal. I cannot get my result: Cpi@raspberrypi:~/Voice $ ./test.sh | more. I get "stop" stop.
                – rfid gao
                Jun 25 '16 at 2:57

















              • Thank all the people's help here. I had try Zachary's code. The issue is same. $QUESTION is "stop", $command is stop, so they cannot be equal. I cannot get my result: Cpi@raspberrypi:~/Voice $ ./test.sh | more. I get "stop" stop.
                – rfid gao
                Jun 25 '16 at 2:57
















              Thank all the people's help here. I had try Zachary's code. The issue is same. $QUESTION is "stop", $command is stop, so they cannot be equal. I cannot get my result: Cpi@raspberrypi:~/Voice $ ./test.sh | more. I get "stop" stop.
              – rfid gao
              Jun 25 '16 at 2:57





              Thank all the people's help here. I had try Zachary's code. The issue is same. $QUESTION is "stop", $command is stop, so they cannot be equal. I cannot get my result: Cpi@raspberrypi:~/Voice $ ./test.sh | more. I get "stop" stop.
              – rfid gao
              Jun 25 '16 at 2:57











              up vote
              1
              down vote













              grep -q '"stop"' < in > /dev/null && echo hooray





              share|improve this answer


























                up vote
                1
                down vote













                grep -q '"stop"' < in > /dev/null && echo hooray





                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  grep -q '"stop"' < in > /dev/null && echo hooray





                  share|improve this answer














                  grep -q '"stop"' < in > /dev/null && echo hooray






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 21 '16 at 12:00









                  Anthon

                  59.8k17102163




                  59.8k17102163










                  answered Sep 21 '16 at 11:15









                  mikeserv

                  45k566152




                  45k566152



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f291990%2fhow-to-compare-abc-with-abc-in-a-shell-script%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

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?