In: Computer Science
In shell script what is the reason this statement is not working?
x=7
y=13
while [ $x -le $y ]
do
echo $x
x=x+2
done
why on while statement it has error?
2.x=7
y=13
while [$x \< $y ]
do
echo $x
let x=x+2
done
what does it mean \< ? and give me the reason how this statement works
Question 1:
Issue Description:
if you run the given script, you ll get an error "Integer expression expected"
Answer: It is because of bad arithmetic expansion. you have to use $ and the expression should be inside double brackets $((expression))
This program assigns x with 7 and y with 13 initially. x is incremented by two for each iteration and prints x value until x exceeds y i.e, 13.
The script should be as follows:
x=7
y=13
while [ $x -le $y ]
do
echo $x
x=$((x+2))
done
Sample Output:
Question 2: what does it mean \< ? and give me the reason how this statement works
Answer: There is no such operator \< in shell scripting. If you run the script with '\<', you ll get an error. See the output below,
you have to use the below operators as appropriate.
I have replaced the operator \< by -le, see the script and output below.
x=7
y=13
while [ $x -le $y ]
do
echo $x
let x=x+2
done
Sample output: