[英]How to check if there are more than 5 inputs given to read in bash?
I want to take no more than 5 inputs, if there are more than 5 space separated inputs then it should ask for input again and display message "you have entered more than 5 inputs"我想输入不超过 5 个输入,如果有超过 5 个空格分隔的输入,那么它应该再次要求输入并显示消息“您输入的输入超过 5 个”
while true
do
echo "enter marks of 5 subjects"
read m1 m2 m3 m4 m5
if [ $m1 -le 100 ] && [ $m2 -le 100 ] && [ $m3 -le 100 ] && [ $m4 -le 100 ] && [ $m5 -le 100 ]
then
break
else
echo "marks cannot be more than 100"
fi
done
I would instead read into an array:我会改为读入一个数组:
read -a m
You can then access the members as然后,您可以访问成员
${m[0]} ${m[1]} ${m[2]}
and so on...等等...
And you can access the size of the array with你可以访问数组的大小
${#m[@]}
which can be used in testing whether you have too many (or too few) inputs.可用于测试您是否有太多(或太少)输入。
Add m6
to read m1 m2 m3 m4 m5
and check if m6
is empty or not.添加
m6
read m1 m2 m3 m4 m5
并检查m6
是否为空。
if [[ "$m6" != "" ]]; then echo "too many arguments"; else echo "ok"; fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.