read是linux的一个内置变量,常用的就是-p -t
-p 设置提示信息
-t 设置输入等待的时间(默认是秒单位的)如果超过等待时间还没有输入就会自动退出
比如说
[root@localhost ~]# read -p "this is test please input two int:" number1 number2
this is test please input two int:1 2
[root@localhost ~]# echo $number1
1
[root@localhost ~]# echo $number2
2
[root@localhost ~]#
上面的1和2是用户输入的,用户输入之后就会被赋值给number1,number2这两个变量
下面我用read写了一个计算器,有点小BUG,如果有大牛知道怎么解决可以说一下,谢谢哈
#!/bin/bash
while :
do
read -p "please input the number:" a
expr $a + 0 &>/dev/null
if [ $? -eq 0 ]
then
echo "you input the number is:" $a
break
else
echo "you input is not number,please replace input:"
fi
done
while :
do
read -p "please input the number2:" b
expr $b + 0 &>/dev/null
if [ $? -eq 0 ]
then
echo "you input the number2 is:" $b
break
else
echo "you input is not number,please replace input:"
fi
done
echo "------------"
echo "| |"
echo "| 1.加法 |"
echo "| |"
echo "| 2.减法 |"
echo "| |"
echo "| 3.乘法 |"
echo "| |"
echo "| 4.除法 |"
echo "| |"
echo "------------"
while :
do
read -p "please use 1,2,3,4 to compute:" me
if [ $me -eq "1" ] || [ $me -eq "2" ] || [ $me -eq "3" ] || [ $me -eq "4" ]
then
echo "you input is:" $me
break
else
echo "you input is no 1 2 3 4 please replace input"
fi
done
case $me in
1)
sum=`expr $a + $b`
echo "$a + $b = $sum"
;;
2)
jian=`expr $a - $b`
echo "$a - $b = $jian"
;;
3)
chen=`expr $a \* $b`
echo "$a * $b = $chen"
;;
4)
chu=`expr $a \/ $b`
echo "$a / $b = $chu"
esac
本文转自 周子琪 51CTO博客,原文链接:http://blog.51cto.com/izhouyu/1890158