iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >Shell脚本编程中常用的数学运算实例
  • 120
分享到

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

脚本实例常用 2022-06-04 21:06:49 120人浏览 泡泡鱼
摘要

这部分主要讨论数学相关的shell脚本编程。 加法运算 新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。 #!/bin/bash echo “Enter the First

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

加法运算

新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。

#!/bin/bash

echo “Enter the First Number: ”

read a

echo “Enter the Second Number: ”

read b

x=$(expr "$a" + "$b")

echo $a + $b = $x

输出结果:
[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

减法运算

#!/bin/bash

echo “Enter the First Number: ”

read a

echo “Enter the Second Number: ”

read b

x=$(($a - $b))

echo $a - $b = $x

注意:这里我们没有像上面的例子中使用“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

乘法运算

#!/bin/bash

echo “Enter the First Number: ”

read a

echo “Enter the Second Number: ”

read b

echo "$a * $b = $(expr $a * $b)"

输出结果:
[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

除法运算

#!/bin/bash

echo “Enter the First Number: ”

read a

echo “Enter the Second Number: ”

read b

echo "$a / $b = $(expr $a / $b)"

输出结果:
[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

数组

下面的这个脚本可以打印一组数字。

#!/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

输出结果:
[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

你可以从这里下载这个例子的代码

判断奇偶数

#!/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

输出结果:
[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

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

输出结果:
[root@tecmint ~]# vi Factorial.sh

[root@tecmint ~]# chmod 755 Factorial.sh

[root@tecmint ~]# ./Factorial.sh

 

Enter The Number

12

479001600

你可以从这里下载这个例子的代码

判断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

echo "Not Armstrong"

fi

输出结果:
[root@tecmint ~]# vi Armstrong.sh

[root@tecmint ~]# chmod 755 Armstrong.sh

[root@tecmint ~]# ./Armstrong.sh

 

Enter A Number

371

371

Armstrong

1

2

3

4

5

6

[root@tecmint ~]# ./Armstrong.sh

 

Enter A Number

123

36

Not Armstrong

判断质数

#!/bin/bash

echo “Enter Any Number”

read n

i=1

c=1

while [ $i -le $n ]

do

i=$(expr $i + 1)

r=$(expr $n % $i)

if [ $r -eq 0 ]

then

c=$(expr $c + 1)

fi

done

if [ $c -eq 2 ]

then

echo “Prime”

else

echo “Not Prime”

fi

输出结果:
[root@tecmint ~]# vi Prime.sh

[root@tecmint ~]# chmod 755 Prime.sh

[root@tecmint ~]# ./Prime.sh

 

“Enter Any Number”

12 

 

“Not Prime”

--结束END--

本文标题: Shell脚本编程中常用的数学运算实例

本文链接: https://www.lsjlt.com/news/17749.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Shell脚本编程中常用的数学运算实例
    这部分主要讨论数学相关的shell脚本编程。 加法运算 新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。 #!/bin/bash echo “Enter the First ...
    99+
    2022-06-04
    脚本 实例 常用
  • Shell脚本编程中常用的数学运算方法教程
    这篇文章主要介绍“Shell脚本编程中常用的数学运算方法教程”,在日常操作中,相信很多人在Shell脚本编程中常用的数学运算方法教程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Shell脚本编程中常用的数学...
    99+
    2023-06-09
  • Shell脚本处理浮点数的运算和比较实例
    通过top命令看到的进程的CPU、内存的使用率的百分比是一个浮点数,我需要在写脚本时对其进行处理,所以学习了一些,总结如下。 其实,Shell(这里是Bash)本身不具备处理浮点计算的能力,但是可以使用“b...
    99+
    2022-06-04
    脚本 实例 浮点数
  • shell脚本编程中case语句的实例用法
    本篇内容介绍了“shell脚本编程中case语句的实例用法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!case语句是用来实现多个if..e...
    99+
    2023-06-09
  • Python编程实现数学运算求一元二次方程的实根算法示例
    本文实例讲述了Python编程实现数学运算求一元二次方程的实根算法。分享给大家供大家参考,具体如下: 问题: 请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax&sup...
    99+
    2022-06-04
    实根 示例 算法
  • 详解Python编程中基本的数学计算使用
    数 在 Python 中,对数的规定比较简单,基本在小学数学水平即可理解。 那么,做为零基础学习这,也就从计算小学数学题目开始吧。因为从这里开始,数学的基础知识列位肯定过关了。 >>>...
    99+
    2022-06-04
    详解 数学 Python
  • 如何使用Go语言并发编程处理Shell脚本中的大数据?
    随着数据量的不断增大,传统的Shell脚本处理方式已经不能满足现代数据处理的需求。而Go语言则是一种高效的并发编程语言,可以很好地处理大数据量的问题。本文将介绍如何使用Go语言并发编程处理Shell脚本中的大数据。 一、Go语言的并发编程...
    99+
    2023-06-27
    并发 shell 大数据
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作