-3

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"

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
rfid gao
  • 19
  • 2
  • *"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 00: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?](http://unix.stackexchange.com/q/131766/135943) – Wildcard Jun 25 '16 at 01:01
  • The result is $QUESTIONS is "stop", $command is stop, It cannot compare with eachother – rfid gao Jun 25 '16 at 03: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 03:05

3 Answers3

2

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
AReddy
  • 3,122
  • 5
  • 35
  • 75
rfid gao
  • 19
  • 2
1
#!/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"\!
    
Zachary Brady
  • 4,200
  • 2
  • 17
  • 40
  • 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 02:57
1
grep -q '"stop"' < in > /dev/null && echo hooray
Anthon
  • 78,313
  • 42
  • 165
  • 222
mikeserv
  • 57,448
  • 9
  • 113
  • 229