read [参数][变量名]
read -p "please input a number:"var
read -t 10 -p "please input a number:"var
#!/bin/sh
while true
do
read -t 10 -p "please input two number:" a b
expr $a + 0 >/dev/null 2>&1
[ $? -ne 0 ] && continue
expr $b + 0 >/dev/null 2>&1
[ $? -ne 0 ] && continue||break
done
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 ))"
传参:
[root@www shell]# cat read1.sh
#!/bin/sh
a="$1"
b="$2"
Usage(){
echo "Usage:sh $0 num1 num2"
exit 1
}
if [ $# -ne 2 ];then
Usage
fi
expr $a + 0 >/dev/null 2>&1
[ $? -ne 0 ] && Usage
expr $b + 0 >/dev/null 2>&1
[ $? -ne 0 ] && Usage
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 ))"
原文:http://butterflykiss.blog.51cto.com/3354010/1951612