最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Shell腳本編程中常用的數(shù)學(xué)運(yùn)算實例

 更新時間:2014年06月21日 11:53:59   投稿:junjie  
這篇文章主要介紹了Shell腳本編程中常用的數(shù)學(xué)運(yùn)算實例,包含最基本的加減乘除,還有質(zhì)數(shù)、偶數(shù)的判斷等,需要的朋友可以參考下

這部分主要討論數(shù)學(xué)相關(guān)的shell腳本編程。

加法運(yùn)算

新建一個文件“Addition.sh”,輸入下面的內(nèi)容并賦予其可執(zhí)行的權(quán)限。

復(fù)制代碼 代碼如下:
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(expr "$a" + "$b")
echo $a + $b = $x

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

減法運(yùn)算

復(fù)制代碼 代碼如下:

#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(($a - $b))
echo $a - $b = $x

注意:這里我們沒有像上面的例子中使用“expr”來執(zhí)行數(shù)學(xué)運(yùn)算。

輸出結(jié)果:

復(fù)制代碼 代碼如下:

[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

乘法運(yùn)算

復(fù)制代碼 代碼如下:

#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a * $b = $(expr $a \* $b)"

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

除法運(yùn)算

復(fù)制代碼 代碼如下:

#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a / $b = $(expr $a / $b)"

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

數(shù)組

下面的這個腳本可以打印一組數(shù)字。

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

你可以從這里下載這個例子的代碼

判斷奇偶數(shù)

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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數(shù)

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:
[root@tecmint ~]# vi Factorial.sh
[root@tecmint ~]# chmod 755 Factorial.sh
[root@tecmint ~]# ./Factorial.sh
 
Enter The Number
12
479001600

你可以從這里下載這個例子的代碼

判斷Armstrong數(shù)

Armstrong數(shù):在三位的正整數(shù)中,例如abc,有一些可能滿足(a^3)+(b^3)+(c^3)=abc,即各個位數(shù)的立方和正好是該數(shù)的本身。這些數(shù)即稱為Armstrong數(shù)。

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

判斷質(zhì)數(shù)

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[root@tecmint ~]# vi Prime.sh
[root@tecmint ~]# chmod 755 Prime.sh
[root@tecmint ~]# ./Prime.sh
 
“Enter Any Number”
12
 
“Not Prime”

相關(guān)文章

  • 基于Shell中for循環(huán)的幾個常用寫法分享

    基于Shell中for循環(huán)的幾個常用寫法分享

    今天小編就為大家分享一篇基于Shell中for循環(huán)的幾個常用寫法分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Linux awk將文件某列按照逗號分隔的例子

    Linux awk將文件某列按照逗號分隔的例子

    今天小編就為大家分享一篇關(guān)于Linux awk將文件某列按照逗號分隔的例子,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 一天一個shell命令 linux文本操作系列-tree命令詳解

    一天一個shell命令 linux文本操作系列-tree命令詳解

    這篇文章主要介紹了一天一個shell命令 linux文本操作系列-tree命令詳解,需要的朋友可以參考下
    2016-06-06
  • ubuntu編譯pyav報錯libx264?not?found解決示例

    ubuntu編譯pyav報錯libx264?not?found解決示例

    這篇文章主要為大家介紹了ubuntu編譯pyav報錯libx264?not?found解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • shell中case的用法學(xué)習(xí)筆記

    shell中case的用法學(xué)習(xí)筆記

    這篇文章主要為大家介紹shell中的case語句:可以把變量的內(nèi)容與多個模板進(jìn)行匹配,再根據(jù)成功匹配的模板去決定應(yīng)該執(zhí)行哪部分代碼
    2013-11-11
  • shell中數(shù)組的定義及操作

    shell中數(shù)組的定義及操作

    本文主要介紹了shell中數(shù)組的定義及操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Linux中修改文件權(quán)限chmod命令詳解

    Linux中修改文件權(quán)限chmod命令詳解

    在Linux系統(tǒng)中,chmod命令用于更改文件或目錄的權(quán)限,它可以授予或撤銷對文件的讀取、寫入和執(zhí)行權(quán)限,本文給大家詳細(xì)的介紹了Linux修改文件權(quán)限chmod命令用法,需要的朋友可以參考下
    2023-08-08
  • linux查看已使用內(nèi)存的常用命令

    linux查看已使用內(nèi)存的常用命令

    在Linux系統(tǒng)中,我們可以使用多個命令來查看內(nèi)存使用情況,其中比較常用的命令有free、top、ps和sar,使用free命令可以查看系統(tǒng)內(nèi)存的使用情況,這些命令可以幫助我們快速了解系統(tǒng)內(nèi)存的使用情況,從而更好地進(jìn)行內(nèi)存管理和優(yōu)化,需要的朋友可以參考下
    2024-01-01
  • Shell腳本中的printf命令使用

    Shell腳本中的printf命令使用

    本文主要介紹了Shell腳本中的printf命令使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Shell中的循環(huán)語句for、while、until實例講解

    Shell中的循環(huán)語句for、while、until實例講解

    這篇文章主要介紹了Shell中的循環(huán)語句for、while、until實例講解,簡單清晰明了,非常不錯的教程,需要的朋友可以參考下
    2014-06-06

最新評論

霸州市| 大邑县| 新民市| 涟水县| 富源县| 彭山县| 崇文区| 青阳县| 城口县| 兰州市| 松江区| 鞍山市| 海林市| 突泉县| 青浦区| 闸北区| 临邑县| 南平市| 鄂托克旗| 西峡县| 万年县| 射洪县| 三亚市| 齐河县| 南和县| 津南区| 河北区| 清丰县| 莆田市| 永清县| 清镇市| 三门峡市| 苍山县| 刚察县| 德兴市| 阿瓦提县| 榆林市| 肇州县| 航空| 东兴市| 长泰县|