shell中read的使用

前端之家收集整理的这篇文章主要介绍了shell中read的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 ))"

猜你在找的Bash相关文章