shell基础:使用read、命令行脚本传参实现输入2个整数并计算

简介:

shell基础练习题:使用read交互输入,命令行脚本传参2种方式,实现输入2个整数数字,并计算加减乘除。考察shell基础知识包括:变量定义、read、if判断语句、正则表达式等知识;


第一种方式:read交互输入参数

思路为:判断输入的第2个变量是否为空,为空则提示输入2个数字;不为空则判断输入的是否为整数,用到expr,作用为让2个变量进行相加,如果结果为0说明输入2个为数字,如结果非0则说明输入非整数,提示输入的不是非整数;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
read  -p  "pls input two number:"  a b
if   [ ! -z $b ]
then
      expr  $a + $b &> /dev/null
      if  [ $? - eq  0 ]
       then
           echo  "a-b=$(($a-$b))"
           echo  "a+b=$(($a+$b))"
           echo  "a*b=$(($a*$b))"
           echo  "a/b=$(($a/$b))"
          else
               echo  "input is not zhengshu"
          fi
else
      echo  "you must input two number"
fi

执行结果如下:输入字母会报错,输入不是整数;只输入1个参数也会报错;

1
2
3
4
5
6
7
8
9
10
11
12
[baby@localhost ~]$ sh a.sh
pls input two number:4 2
a-b=2
a+b=6
a*b=8
a /b =2
[baby@localhost ~]$ sh a.sh
pls input two number:a 2
input is not zhengshu
[baby@localhost ~]$ sh a.sh
pls input two number:10
you must input two number


脚本有bug,如果输入3个参数的话会报错如下:

[baby@localhost ~]$ sh a.sh

pls input two number:1 3 3

a.sh: line 3: [: 3: binary operator expected

you must input two number



针对上面的脚本bug修改如下:

思路为:多添加一个变量c,并多了if判断,先判断$a是否为空,如为空提示输入2个数字并退出;然后判断$b是否为空,如未空提示输入2个数字并退出;只有$a $b都不为空即都有输入值,再判断$c是否为空,如未空执行下面的整数判断,如$c不为空同样提示输入2个数字并退出;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
read  -p  "pls input two number:"  a b c
if  [ -z $a ]
   then
  echo  "you must input two number"
  exit
elif  [ -z $b ]
   then
  echo  "you must input two number"
  exit
fi
if  [ -z $c ]
then
  expr  $a + $b &> /dev/null
  if  [ $? - eq  0 ]
   then
   echo  "a-b=$(($a-$b))"
   echo  "a+b=$(($a+$b))"
   echo  "a*b=$(($a*$b))"
   echo  "a/b=$(($a/$b))"
  else 
   echo  "input is not zhengshu"
  fi
else
  echo  "you must input two number"
fi


执行结果如下,什么都不输入,输入一个字符都会提示必须输入2个数字;输入2个值中有字母提示输入的非整数;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[baby@localhost ~]$ sh a.sh  
pls input two number:
you must input two number
[baby@localhost ~]$ sh a.sh
pls input two number:1
you must input two number
[baby@localhost ~]$ sh a.sh
pls input two number:1 a
input is not zhengshu
[baby@localhost ~]$ sh a.sh
pls input two number:2 1
a-b=1
a+b=3
a*b=2
a /b =2


第二种方式:命令行脚本传参方式

思路为:定义a b两个变量,接受命令行传递的参数;$#为输入参数的总个数;判断输入的参数个数不等于2,则提示必须输入2个数字;等于2的话执行下面的脚本;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[baby@localhost ~]$  cat  b.sh
#!/bin/bash
a=$1
b=$2
if  [ $ # -ne 2 ]
then
  echo  "you must input two number"
  exit  1
else
  expr  $a + $b &> /dev/null
  if  [ $? - eq  0 ]
   then
   echo  "a-b=$(($a-$b))"
   echo  "a+b=$(($a+$b))"
   echo  "a*b=$(($a*$b))"
   echo  "a/b=$(($a/$b))"
  else
   echo  "input is not zhengshu"
   exit  1
  fi
fi


执行结果如下:传参为空,参数为3个都会提示必须输入2个数字;传参包含非数字则提示输入的不是整数;

1
2
3
4
5
6
7
8
9
[baby@localhost ~]$ sh b.sh 3 a
input is not zhengshu
[baby@localhost ~]$ sh b.sh 3 2 3
you must input two number
[baby@localhost ~]$ sh b.sh 3 2
a-b=1
a+b=5
a*b=6
a /b =1


总结:

read可以和用户交互性较好,脚本较为复杂多了if判断效率不高;

命令行传参使用表达式判断输入参数,执行效率和理解性较好;






本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1702758,如需转载请自行联系原作者
目录
相关文章
|
1天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
22 3
|
5天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
23 0
|
6天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
7天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
23 1
|
11天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
140 0
|
11天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
133 0
|
15天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
108 0
|
Shell Perl 人工智能
【shell 】 使用 shell 计算成绩
root@client.example.com # cat 1.txt jack huaxue 90tom  huaxue 86jack shuxue 99tom  shuxue 80要求算出jack和tom的2科的平均分  用shell实现。
782 0
|
22天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
104 1
|
24天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
16 0