2

I am trying to check a string that is output from a program, if the string matches a certain content, the while-loop will stop the program. At the same time, I need to count how many times the program has run:

x = "Lookup success"  # this is supposed to be the output from the program
INTERVAL=0  # count the number of runs

while ["$x" != "Lookup failed"]   # only break out the while loop when "Lookup failed" happened in the program
do
   echo "not failed"     # do something
   $x = "Lookup failed"     # just for testing the break-out
   INTERVAL=(( $INTERVAL + 10 )); # the interval increments by 10  
done

echo $x
echo $INTERVAL

But this shell script is not working, with this error:

./test.sh: line 9: x: command not found 
./test.sh: line 12: [[: command not found 

Could someone help me please? I appreciate your help.

1
  • Kindly accept or upvote the answers if it helped you. Commented Dec 9, 2013 at 6:37

3 Answers 3

4

You need spaces around the [ command name. You also need a space before the ] argument at the end of the command.

You also cannot have spaces around assignments in shell. And your assignment in the loop does not need a $ at the start.

x="Lookup success"
INTERVAL=0  # count the number of runs

while [ "$x" != "Lookup failed" ]
do
    echo "not failed"
    x="Lookup failed"
    INTERVAL=(( $INTERVAL + 10 ))  
done

echo $x
echo $INTERVAL
Sign up to request clarification or add additional context in comments.

Comments

3

Not sure if there's a shell that would accept INTERVAL=((...)); my version of ksh and bash on two platforms does not. INTERVAL=$((...)) does work:

#!/bin/bash

x="Lookup success"
INTERVAL=0  # count the number of runs

while [ "$x" != "Lookup failed" ]
do
    echo "not failed"
    x="Lookup failed"
    INTERVAL=$(( $INTERVAL + 10 ))  
done

echo $x
echo $INTERVAL

Credits go to @JonathanLeffler. I'll appreciate up-votes so that next time I don't have to copy-paste others' solution for pointing out a simple typo (comment rights start with rep>=50).

Comments

2

Add a space after [ and before ].

Also, as Jonathan said, you cannot have space in assignments as well.

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.