偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Linux Shell腳本語(yǔ)言與數(shù)學(xué)表達(dá)式

譯文
系統(tǒng) Linux
當(dāng)你理解了Shell腳本,每當(dāng)需要時(shí)都能流暢編寫(xiě)時(shí),那種感覺(jué)很爽的。本章中,我們將教你用腳本語(yǔ)言進(jìn)行比較復(fù)雜的數(shù)學(xué)運(yùn)算。

【51CTO譯文】當(dāng)你理解了Shell腳本,每當(dāng)需要時(shí)都能流暢編寫(xiě)時(shí),那種感覺(jué)很爽的。本章中,我們將教你用腳本語(yǔ)言進(jìn)行比較復(fù)雜的數(shù)學(xué)運(yùn)算。

讓我們從斐波那契數(shù)列開(kāi)始吧。

斐波那契數(shù)列,又稱(chēng)黃金分割數(shù)列,指的是這樣一個(gè)數(shù)列:0、1、1、2、3、5、8、13、21……,它的每一項(xiàng)都是前兩項(xiàng)的和,定義數(shù)列的首兩項(xiàng)為0、1。

腳本1:Fibonacci.sh

#!/bin/bash
echo "How many numbers do you want of Fibonacci series ?" 
  read total 
  x=0 
  y=1 
  i=2 
  echo "Fibonacci Series up to $total terms :: " 
  echo "$x" 
  echo "$y" 
  while [ $i -lt $total ] 
  do 
      i=`expr $i + 1 ` 
      z=`expr $x + $y ` 
      echo "$z" 
      x=$y 
      y=$z 
  done

示例輸出

[root@tecmint ~]# chmod 755 Fibonacci.sh
[root@tecmint ~]# ./Fibonacci.sh

How many numbers do you want of Fibonacci series ? 
10 
Fibonacci Series up to 10 terms :: 
0 
1 
1 
2 
3 
5 
8 
13 
21 
34

下載Fibonacci.sh

#p#

想必大家都清楚,計(jì)算機(jī)只能理解二進(jìn)制格式,即0和1,大多數(shù)人都喜歡學(xué)習(xí)十進(jìn)制與二進(jìn)制的轉(zhuǎn)換。不如為這個(gè)復(fù)雜的計(jì)算編寫(xiě)一個(gè)簡(jiǎn)單的腳本吧。

腳本2:DecimalToBinary.sh

#!/bin/bash 

for ((i=32;i>=0;i--)); do 
        r=$(( 2**$i)) 
        Probablity+=( $r  ) 
done 

[[ $# -eq 0 ]] &echo -en "Decimal\t\tBinary\n" 
for input_int in $@; do 
s=0 
test ${#input_int} -gt 11 &printf "%-10s\t" "$input_int" 

        for n in ${Probablity[@]}; do 

                if [[ $input_int -lt ${n} ]]; then 
                        [[ $s = 1 ]] && printf "%d" 0 
                else 
                        printf "%d" 1 ; s=1 
                        input_int=$(( $input_int - ${n} )) 
                fi 
        done 
echo -e 
done

示例輸出

[root@tecmint ~]# chmod 755 Decimal2Binary.sh
[root@tecmint ~]# ./Decimal2Binary.sh 1121

Decimal		Binary 
1121      	10001100001

注意:上面的腳本在運(yùn)行時(shí)接受輸入,這對(duì)我們來(lái)說(shuō)無(wú)疑是個(gè)好幫手。

下載DecimalToBinary.sh

其實(shí),內(nèi)置的“bc”命令僅用簡(jiǎn)單的一行代碼就能將十進(jìn)制數(shù)轉(zhuǎn)成二進(jìn)制。運(yùn)行如下腳本:

[root@tecmint ~]# echo "obase=2; NUM" | bc

NUM是你想要轉(zhuǎn)換的十進(jìn)制數(shù),比如,

[root@tecmint ~]# echo "obase=2; 121" | bc 

1111001

#p#

接下來(lái)是另一個(gè)腳本,功能與上述腳本恰恰相反,即將二進(jìn)制值轉(zhuǎn)成十進(jìn)制。

腳本3:BinaryToDecimal.sh

#!/bin/bash 
echo "Enter a number :" 
read Binary 
if [ $Binary -eq 0 ] 
then 
echo "Enter a valid number " 
else 
while [ $Binary -ne 0 ] 
do 
Bnumber=$Binary 
Decimal=0 
power=1 
while [ $Binary -ne 0 ] 
do 
rem=$(expr $Binary % 10 ) 
Decimal=$((Decimal+(rem*power))) 
power=$((power*2)) 
Binary=$(expr $Binary / 10) 
done 
echo  " $Decimal" 
done 
fi

示例輸出

[root@tecmint ~]# chmod 755 Binary2Decimal.sh
[root@tecmint ~]# ./Binary2Decimal.sh

Enter a number : 
11 
3

注意:上述功能在終端也能用“bc”命令達(dá)成,如下,

[root@tecmint ~]# echo "ibase=2; BINARY" | bc

BINARY是你想轉(zhuǎn)換的二進(jìn)制數(shù),例如,

[root@tecmint ~]# echo "ibase=2; 11010101" | bc 

213

下載BinaryToDecimal.sh

#p#

同樣,你自己也能編寫(xiě)八進(jìn)制、十六進(jìn)制轉(zhuǎn)換成十進(jìn)制,反之亦然。用“bc”命令完成這些轉(zhuǎn)換的代碼如下,

十進(jìn)制轉(zhuǎn)八進(jìn)制

[root@tecmint ~]# echo "obase=8; Decimal" | bc

十進(jìn)制轉(zhuǎn)十六進(jìn)制

[root@tecmint ~]# echo "obase=16; Decimal" | bc

八進(jìn)制轉(zhuǎn)十進(jìn)制

[root@tecmint ~]# echo "ibase=8; Octal" | bc

十六進(jìn)制轉(zhuǎn)十進(jìn)制

[root@tecmint ~]# echo "ibase=16; Hexadecimal" | bc

二進(jìn)制轉(zhuǎn)八進(jìn)制

[root@tecmint ~]# echo "ibase=2;obase=8 Binary" | bc

#p#

Shell腳本語(yǔ)言中一些常用的數(shù)值描述如下,

Test : INTEGER1 -eq INTEGER2
Meaning: INTEGER1 is equal to INTEGER2
Test : INTEGER1 -ge INTEGER2
Meaning: INTEGER1 is greater than or equal to INTEGER2
Test: INTEGER1 -gt INTEGER2
Meaning: INTEGER1 is greater than INTEGER2
Test:INTEGER1 -le INTEGER2
Meaning: INTEGER1 is less than or equal to INTEGER2
Test: INTEGER1 -lt INTEGER2
Meaning: INTEGER1 is less than INTEGER2
Test: INTEGER1 -ne INTEGER2
Meaning: INTEGER1 is not equal to INTEGER2

原文鏈接:http://www.tecmint.com/calculating-mathematical-expressions-in-shell-scripting-part-v/

 

責(zé)任編輯:奔跑的冰淇淋 來(lái)源: 51CTO.com
相關(guān)推薦

2022-01-04 11:35:03

Linux Shel正則表達(dá)式Linux

2013-06-03 09:45:53

R語(yǔ)言

2009-06-10 08:41:15

Linux腳本語(yǔ)言

2009-12-17 10:39:01

Ruby數(shù)學(xué)表達(dá)式

2012-07-16 11:23:18

LinuxWindows腳本語(yǔ)言

2024-03-25 13:46:12

C#Lambda編程

2013-01-05 02:19:50

JavaLambda表達(dá)式JVM

2009-09-16 09:58:53

PHP正則表達(dá)式函數(shù)

2017-05-12 10:47:45

Linux正則表達(dá)式程序基礎(chǔ)

2017-12-27 15:25:50

LinuxBash正則表達(dá)式

2014-01-05 17:41:09

PostgreSQL表達(dá)式

2022-09-08 11:35:45

Python表達(dá)式函數(shù)

2022-09-09 00:25:48

Python工具安全

2023-07-31 07:43:07

Java語(yǔ)言表達(dá)式變量

2020-11-16 11:10:00

ShellLinux正則表達(dá)式

2022-05-07 12:21:14

工具分布式

2010-03-09 11:15:28

Python語(yǔ)言教程

2011-08-22 18:00:53

Lua語(yǔ)言

2022-08-29 23:49:41

Python字符串代碼

2010-03-03 13:31:25

Linux正則表達(dá)式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)