shell变量的输入:
shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入获取,read为bash内置命令,可以通过help read查看帮助。
语法格式:
read 【参数】 【变量名】
常用参数:
-p :设置提示信息
-t :设置输入等待的时间,单位默认为秒。
测试:read的基本读入
![](https://img-blog.csdnimg.cn/img_convert/2c6dfb1dfcc298022723292724ba9d07.png)
read -t 5 -p "Pls input two number:" a1 a2 #超时5秒后退出
例如:我们可以写一个加减乘除计算脚本,以read方式读入整数变量:
简单脚本:
1 [root@bqh-117 scripts]# cat js.sh
2 #!bin/sh
3 read -t 15 -p "input two number:" a b
4 echo $a-$b=$(($a-$b))
5 echo $a+$b=$(($a+$b))
6 echo $a*$b=$(($a*$b))
7 echo $a/$b=$(($a/$b))
8 echo $a**$b=$(($a**$b))
9 echo $a%$b=$(($a%$b))
10 [root@bqh-117 scripts]# sh js.sh
11 input two number:8 4
12 8-4=4
13 8+4=12
14 8*4=32
15 8/4=2
16 8**4=4096
17 8%4=0
定义变量是也可以:
read –t 15 –p ” please input two number:” num1 num2
a=$num1
b=$num2
利用echo命令替代read –p的功能:
echo -n ” please input two number:” a b
read a b
思考:如果前面的范例脚本中读入的不是整数或者输入的数字个数不是2个,执行脚本会什么结果?如何解决?
解答:
上述程序可以如下思考解决:
可以模拟打游戏过关的思路,设置关卡,满足一项要求则过关,否则,game over!
第一关:用户按要求输入了为两个值则过关,否则,game over。
编程提示:可以用变量的字符串长度判断,例如:${#bqh}
第二关:用户输入的内容均为整数才过关,否则 game over
编程提示:用expr特殊技巧进行判断
第三关:当读入的参数符合个数和整数条件后,进行计算。
----------------------------------------------------------------------------------------------------------------------
方法一:read读入
1 [root@bqh-117 scripts]# vim read_js.sh
2
3 #/bin/sh
4 read -t 8 -p "please input two number:" a b #a前面要有空格
5 #no.1
6 [ -z "$a" ]&&{ echo "first number must be int."
7 exit 1
8 }
9 [ -z "$b" ]&&{ echo "second number must be int."
10 exit 2
11 }
12 #no.2
13 expr $a + $b + 1 &>/dev/null
14 [ $? -ne 0 ]&&{ echo "the number you input must be int."
15 exit 3
16 }
17 #no.3
18 echo "$a-$b =$(($a-$b))"
19 echo "$a+$b =$(($a+$b))"
20 echo "$a*$b =$(($a*$b))"
21 if [ $b -eq 0 ];then
22 echo "fenmu can not be 0,s0 /,% can not yunsuan."
23 else
24 echo "$a/$b =$(($a/$b))"
25 echo "$a%$b =$(($a%$b))"
26 fi
27 echo "$a**$b =$(($a**$b))"
测试效果如下:
![](https://img-blog.csdnimg.cn/img_convert/31ead0981998cdde793a0b79b8522ba4.png)
方法二:read读入
1 [root@bqh-117 scripts]# vim read_js1.sh
2 #!/bin/sh
3 while true
4 do
5 read -t 8 -p "pls input two number:" a b
6 expr $a + 0 >/dev/null 2>&1
7 [ $? -ne 0 ]&& continue
8 expr $b + 0 >/dev/null 2>&1
9 [ $? -ne 0 ]&& continue || break
10 done
11 echo "$a-$b =$(($a-$b))"
12 echo "$a+$b =$(($a+$b))"
13 echo "$a*$b =$(($a*$b))"
14 echo "$a/$b =$(($a/$b))"
15 echo "$a**$b =$(($a**$b))"
16 echo "$a%$b =$(($a%$b))"
测试效果:
![](https://img-blog.csdnimg.cn/img_convert/5e6d420780ee5f2b110a20682679fc6c.png)
思考:如何将上述read读入改成脚本传参的方式实现?
解答:
方法一:
[root@bqh-117 scripts]# vim read_js3.sh
#!/bin/sh
a=$1
b=$2
Usage() {
echo "USAGE: number 1 number 2"
exit 1
}
if [ $# -ne 2 ];then
Usage
fi
[ "`echo "$a"|sed -r 's#[^0-9]##g'`" = "$a" ]||{
echo "number 1 must be int."
exit 2
}
[ "`echo "$b"|sed -r 's#[^0-9]##g'`" = "$b" ]||{
echo "number 2 must be int."
exit 3
}
echo "$a-$b =$(($a-$b))"
echo "$a+$b =$(($a+$b))"
echo "$a*$b =$(($a*$b))"
echo "$a/$b =$(($a/$b))"
echo "$a**$b =$(($a**$b))"
echo "$a%$b =$(($a%$b))"
测试效果:
![](https://img-blog.csdnimg.cn/img_convert/8951f840d4e33cfe653b6c0db3170ce4.png)
方法二:
1 [root@bqh-117 scripts]# vim read_js2.sh
2 #!/bin/sh
3 a=$1
4 b=$2
5 Usage() {
6 echo "USAGE: number 1 number 2"
7 exit 1
8 }
9 if [ $# -ne 2 ];then
10 Usage
11 fi
12 expr $a + 0 &>/dev/null
13 fetval1=$?
14 expr $b + 0 &>/dev/null
15 fetval2=$?
16 test $fetval1 -eq 0 -a $fetval2 -eq 0 ||{
17 echo "pls input two number 1 and number 2 must be int."
18 Usage
19 }
20 echo "$a-$b =$(($a-$b))"
21 echo "$a+$b =$(($a+$b))"
22 echo "$a*$b =$(($a*$b))"
23 echo "$a/$b =$(($a/$b))"
24 echo "$a**$b =$(($a**$b))"
25 echo "$a%$b =$(($a%$b))"
测试效果:
![](https://img-blog.csdnimg.cn/img_convert/1764367145c262eeee2fc7929d9b37dc.png)
当然还有很多其他方法等等。