0
touch log.txt
while [ 1 ]; do 
 echo "insert data: "
 read data
 if [ $data = "finish" ]; then
  break
 fi
 echo "$data" >> log.txt

When I run the program and type in a word in an input like "hello im martin", it gives me an error saying unexpected operator. How to fix this?

2
  • Do you have a final line written done, right? :) Commented Sep 20, 2014 at 6:56
  • 1
    @FernandoAires: the script wouldn't get as far as running to get the input and then fail on the 'unexpected operator' if the done was missing. Commented Sep 20, 2014 at 7:03

2 Answers 2

3

You're missing a done keyword after your echo statement that signifies the end of the while loop.

Additionally, you should put the $data on line 5 in quotes, otherwise the hello im martin input will cause that line to be interpreted as:

if [ hello im martin = "finish" ]; then
Sign up to request clarification or add additional context in comments.

1 Comment

And, in Bash, the other option is to use the [[ … ]] operator to do the comparison.
0

Try this

while [ 1 ]; do

echo "insert data:"; read data

if [ "$data" == "finish" ]; then

break;

fi

echo "$data" >> log.txt;

done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.