0

I wrote this code:

echo -n "Enter a number1  "
echo -n "Enter a number2  "
read R1
read R2

while [ "$R1" < "$R2"]
do 
if [ $((R1 % 2)) -eq 0 ]; then

$R3=$R1

    echo "Number is $R3"
else
    echo "Nothing"
fi

done

I don't understand why it always give me this error bash: 8]: No such file or directory

1
  • Your specific error message is caused by the lack of space before the closing ] argument, which would need to be inserted in addition to the fix given in the answers. Commented Oct 10, 2013 at 11:55

2 Answers 2

1

You should use -lt instead of <.

while [ "$R1" -lt "$R2" ]

< is interpreted as input redirection in bash.

Or you can use double square brackets to interpret those inside as arithmetic operation:

while [[ "$R1" < "$R2" ]]
Sign up to request clarification or add additional context in comments.

4 Comments

thank you it works but I stuck with infinite loop that disappointed me command not found;number =2;
What do you want to accomplish? Asking user to input two number everytime?
am thinking of Write a Bash shell script that reads as arguments two integer parameters (R1, and R2), if R1 < R2, prints all even numbers between R1 and R2. Otherwise prints out nothing, so I do this
@justhalf I think, you gave the answer first with correct fix+1 ;-)
1

What happens since < "$R2" is intrepreted as read from "$R2". Since you don't have a file with such a name, it complains.

[ (test command) command doesn't have < operator. You have to use -lt instead:

while [ "$R1" -lt "$R2" ]

There's a POSIX extenstion which supports it with a slash:

while [ "$R1" \< "$R2" ]

If you are using bash you bash then you can also use built-in [[ ..]] which has support for <, > etc.

while [[ "$R1" < "$R2" ]]

See also:

What is the difference between test, [ and [[ ?


After re-writing your code to put the loop inside if:

#!/bin/bash
echo -n "Enter a number1  "
read R1
echo -n "Enter a number2  "
read R2

if [[ "$R1" < "$R2" ]]
then
  for((i=R1;i<R2;i++));
  do
    if [[ $((i % 2)) -eq 0 ]]; then
      echo "Number is $i"
    fi
  done
else
    echo "Nothing"
fi

7 Comments

thank you it works but I stuck with infinite loop that disappointed me command not found;number =2
You don't change neither of R1 and R2. Hence, once it enters the loop, it won't break. Depending on what you wanted to do with the code, you may change R1 or R2 or use a break statement on some condition.
am thinking of Write a Bash shell script that reads as arguments two integer parameters (R1, and R2), if R1 < R2, prints all even numbers between R1 and R2. Otherwise prints out nothing, so I do this
I'll give the honor to you Blue Moon, to solve this problem, rather than competing each other. =)
should I make R1,R2 initially equal o zero
|

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.