首页 > 系统服务 > 详细

shell中read的使用

时间:2017-07-28 21:57:54      阅读:258      评论:0      收藏:0      [点我收藏+]

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


shell中read的使用

原文:http://butterflykiss.blog.51cto.com/3354010/1951612

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!