• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell脚本编程中常用的数学运算实例

Shell脚本编程中常用的数学运算实例

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell脚本编程实例,shell脚本实例,linux shell脚本实例,经典shell脚本实例,shell脚本简单实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

这部分主要讨论数学相关的shell脚本编程。

加法运算

新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(expr "$a" + "$b")
echo $a + $b = $x</div>
输出结果:
[root@tecmint ~]# vi Additions.sh
[root@tecmint ~]# chmod 755 Additions.sh
[root@tecmint ~]# ./Additions.sh
 
“Enter the First Number: ”
12
“Enter the Second Number: ”
13
12 + 13 = 25</div>

减法运算

#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(($a - $b))
echo $a - $b = $x</div>
注意:这里我们没有像上面的例子中使用“expr”来执行数学运算。

输出结果:
[root@tecmint ~]# vi Substraction.sh
[root@tecmint ~]# chmod 755 Substraction.sh
[root@tecmint ~]# ./Substraction.sh
 
“Enter the First Number: ”
13
“Enter the Second Number: ”
20
13 - 20 = -7</div>

乘法运算
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a * $b = $(expr $a \* $b)"</div>
输出结果:
[root@tecmint ~]# vi Multiplication.sh
[root@tecmint ~]# chmod 755 Multiplication.sh
[root@tecmint ~]# ./Multiplication.sh
 
“Enter the First Number: ”
11
“Enter the Second Number: ”
11
11 * 11 = 12</div>

除法运算
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a / $b = $(expr $a / $b)"</div>
输出结果:
[root@tecmint ~]# vi Division.sh
[root@tecmint ~]# chmod 755 Division.sh
[root@tecmint ~]# ./Division.sh
 
“Enter the First Number: ”
12
“Enter the Second Number: ”
3
12 / 3 = 4</div>

数组

下面的这个脚本可以打印一组数字。
#!/bin/bash
echo “Enter The Number upto which you want to Print Table: ”
read n
i=1
while [ $i -ne 10 ]
do
i=$(expr $i + 1)
table=$(expr $i \* $n)
echo $table
done</div>
输出结果:
[root@tecmint ~]# vi Table.sh
[root@tecmint ~]# chmod 755 Table.sh
[root@tecmint ~]# ./Table.sh
 
“Enter The Number upto which you want to Print Table: ”
29
58
87
116
145
174
203
232
261
290</div>
你可以从这里下载这个例子的代码

判断奇偶数

#!/bin/bash
echo "Enter The Number"
read n
num=$(expr $n % 2)
if [ $num -eq 0 ]
then
echo "is a Even Number"
else
echo "is a Odd Number"
fi</div>
输出结果:
[root@tecmint ~]# vi EvenOdd.sh
[root@tecmint ~]# chmod 755 EvenOdd.sh
[root@tecmint ~]# ./EvenOdd.sh
 
Enter The Number
12
is a Even Number
1
2
3
4
5
[root@tecmint ~]# ./EvenOdd.sh
 
Enter The Number
11
is a Odd Number</div>

Factorial数

#!/bin/bash
echo "Enter The Number"
read a
fact=1
while [ $a -ne 0 ]
do
fact=$(expr $fact \* $a)
a=$(expr $a - 1)
done
echo $fact</div>
输出结果:
[root@tecmint ~]# chmod 755 Factorial.sh
[root@tecmint ~]# ./Factorial.sh
 
Enter The Number
12
479001600</div>
你可以从这里下载这个例子的代码

判断Armstrong数

Armstrong数:在三位的正整数中,例如abc,有一些可能满足(a^3)+(b^3)+(c^3)=abc,即各个位数的立方和正好是该数的本身。这些数即称为Armstrong数。
#!/bin/bash
echo "Enter A Number"
read n
arm=0
temp=$n
while [ $n -ne 0 ]
do
r=$(expr $n % 10)
arm=$(expr $arm + $r \* $r \* $r)
n=$(expr $n / 10)
done
echo $arm
if [ $arm -eq $temp ]
then
echo "Armstrong"
else

您可能想查找下面的文章:

  • shell脚本 自动创建用户详解及实例代码
  • shell脚本echo输出不换行功能增强实例
  • shell脚本编程之数组
  • Shell脚本检查IP格式及mysql操作实例
  • Shell脚本遍历一个日期范围实例
  • shell脚本中取消重定向的方法实例
  • shell编程中的字符串截取方法小结
  • shell脚本编程实现9*9乘法表
  • shell生成简单格式的xml实例
  • Shell编程 Bash引号的那点事

相关文章

  • Shell脚本编程中常用的数学运算实例
  • 远程SSH连接服务与基本排错经验总结
  • linux下source命令使用详解
  • 脚本实现SSH登录邮件报警
  • Python使用淘宝API查询IP归属地功能分享
  • linux命令行批量创建目录详解
  • Linux下的tar压缩解压缩命令详解(小结)
  • linux命令切换目录的使用方法
  • Shell逐行读取文件的4种方法
  • UNIX sh(Bourne Shell)脚本里面使用数组的两种方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 判断Linux Shell环境变量是否存在
    • real server 的一个启动脚本例子(推荐)
    • 让Linux下的cron以秒为单位执行shell脚本的3种方法
    • shell将脚本输出结果记录到日志文件的实现
    • 实现android自动化测试部署与运行Shell脚本分享
    • shell脚本转发80端口数据包给Node.js服务器
    • Shell脚本判断Linux系统是32位还是64位的几种方法分享
    • Linux 按时间批量删除文件命令(删除N天前文件)
    • C语言实现的ls命令源码分享
    • linux脚本判断条件总结(必看)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有