-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
  • 56,709
  • 26
  • 150
  • 232

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,172
  • 5
  • 36
  • 76
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"\!
    
  • 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
  • 79,293
mikeserv
  • 58,310