while true
do
echo "Enter the number"
read Num
while[$Num -lt 1 || $Num -gt 50]
do
echo "Please enter a new number "
read Num
done
Asked
Active
Viewed 510 times
-1
Michael Homer
- 74,824
- 17
- 212
- 233
-
4Where is second `done`? – Prvt_Yadav Mar 15 '19 at 03:23
-
3And you need a space in between `while` and `[`. There are tons of shell script examples online. – Peschke Mar 15 '19 at 03:27
-
3You also need two spaces between `[` and `$Num` and `50` and `]`. – Nasir Riley Mar 15 '19 at 03:27
-
As it's written, this would give a "command not found" for `while[`, or the whole `while[...]`, or an error about the glob not matching, so the error message in the title and the code in the script don't even match. – ilkkachu Mar 15 '19 at 12:10
1 Answers
3
There are some errors and warnings. Also please use shellcheck.net in these kind of situations.
Errors:
Space between
whileand[.Space after
[and before].Disaster because there is no
done, may be you forgot or you just pasted half of your code.Use
[ a ] || [ b ]instead of[ a || b ].
Warnings:
You should use
read -rinstead ofread.Double quote variable names otherwise this will not work if they have special characters.
Correct Code:
while true
do
echo "Enter the number"
read -r Num
while [ "$Num" -lt 1 ] || [ "$Num" -gt 50 ]
do
echo "Please enter a new number "
read -r Num
done
done
ilkkachu
- 133,243
- 15
- 236
- 397
Prvt_Yadav
- 5,732
- 7
- 32
- 48