While loop in a shell script gives error : [: too many arguments. How to resolve this issue?
While loop in a shell script gives error : [: too many arguments. How to resolve this issue?
function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read upper_limit
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
do
echo "Please enter again."
read_num
done
}
read_num
当我输入两个数字下限和上限时,它会给出以下输出。
check.sh: line 6: [: too many arguments
第 6 行是 while 循环
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
给你,这对我有用:
#!/bin/bash
function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read uper_limit
while [[ $lower_limit -lt 1 ]] || [[ $lower_limit -gt $upper_limit ]]
do
echo "Please enter again."
read_num
done
}
read_num
参考:Bash scripting, multiple conditions in while loop
function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read upper_limit
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
do
echo "Please enter again."
read_num
done
}
read_num
当我输入两个数字下限和上限时,它会给出以下输出。
check.sh: line 6: [: too many arguments
第 6 行是 while 循环
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
给你,这对我有用:
#!/bin/bash
function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read uper_limit
while [[ $lower_limit -lt 1 ]] || [[ $lower_limit -gt $upper_limit ]]
do
echo "Please enter again."
read_num
done
}
read_num
参考:Bash scripting, multiple conditions in while loop