shell 腳本編程 2018-05-31

編程是他媽的一種思想!思想!

1,測試字符串是否爲 null

vim str-test.sh

  1 #!/bin/bash
  2 # str-test.sh: 測試 null 字符串和非引用字符串,
  3 #+ but not strings and sealing wax, not to mention cabbages and kings . . .
  4 #+ 上邊這句沒看懂
  5 # Using if [ ... ]
  6 
  7 
  8 # 如果一個字符串沒被初始化,那麼它就沒有定義的值(像這種話,總感覺像屁話)
  9 # 這種狀態叫做"null"(與 zero 不同)
 10 
 11 if [ -n $string1 ] # $string1 沒被聲明和初始化
 12 then
 13   echo "String \"string1\" is not null."
 14 else
 15   echo "String \"string1\" is null."
 16 fi
 17 # 錯誤的結果.
 18 # 顯示$string1 爲非空,雖然他沒被初始化.
 19 
 20 echo
 21 # 讓我們再試一下.
 22 
 23 if [ -n "$string1" ] # 這次$string1 被引用了. 
 24 then
 25   echo "String \"string1\" is not null."
 26 else
 27   echo "String \"string1\" is null."
 28 fi # ""的字符串在[]結構中
 29 echo
 30 if [ $string1 ] # 這次$string1 變成"裸體"的了
 31 then
 32   echo "String \"string1\" is not null."
 33 else
 34   echo "String \"string1\" is null."
 35 fi
 36 # 這工作得很好.
 37 # 這個[]test 操作檢測 string 是否爲 null.
 38 # 然而,使用("$string1")是一種很好的習慣
 39 #
 40 # As Stephane Chazelas points out,
 41 # if [ $string1 ] 有 1 個參數 "]"
 42 # if [ "$string1" ] 有 2 個參數,空的"$string1"和"]"
 43 echo
 44 
 45 string1=initialized
 46 
 47 if [ $string1 ] # 再來,$string1"裸體了"
 48 then
 49    echo "String \"string1\" is not null."
 50 else
 51   echo "String \"string1\" is null."
 52 fi
 53 # 再來,給出了正確的結果.
 54 # 不過怎麼說("$string1")還是好很多,因爲. . .
 55 
 56 string1="a = b"
 57 
 58 if [ $string1 ] # 再來,$string1 再次裸體了.
 59 then
 60   echo "String \"string1\" is not null."
 61 else
 62   echo "String \"string1\" is null."
 63 fi
 64 # 非引用的"$string1"現在給出了一個錯誤的結果!
 65 exit 0

結果:



2,操作符

最大公約數

vim gcd.sh

  1 # !/bin/bash
  2 # gcd.sh
  3 # 使用使用 Euclid's 算法
  4 
  5 # 最大公約數,就是 2 個數能夠同時整除的最大的數.
  6 
  7 
  8 # Euclid's 算法採用連續除法.
  9 # 在每個循環中
 10 #+ 被除數 <--- 除數
 11 #+ 除數 <--- 餘數
 12 #+ 直到餘數= 0.
 13 #+ 在最後的循環中 The gcd = 被除數
 14 #
 15 # 關於這個算法更精彩的討論
 16 # 見 Jim Loy's site, http://www.jimloy.com/number/euclids.htm.
 17 
 18 
 19 # ------------------------------------------------------
 20 # 參數檢查
 21 ARGS=2
 22 E_BADARGS=65
 23 
 24 if [ $# -ne "$ARGS" ]
 25 then
 26   echo "Usage: `basename $0` first-number second-number"
 27   exit $E_BADARGS
 28 fi
 29 # ------------------------------------------------------ 
gcd ()
 32 {
 33 
 34   dividend=$1 # 隨便給值
 35   divisor=$2 #+ 即使$2 大,也沒關係.
 36   # Why not?
 37 
 38   remainder=1 # 如果再循環中使用爲初始化的變量.
 39   #+ 那將在第一次循環中產生一個錯誤消息.
 40 
 41 
 42   until [ "$remainder" -eq 0 ]
 43   do
 44     let "remainder = $dividend % $divisor"
 45     dividend=$divisor # 現在使用 2 個最小的數重複.
 46     divisor=$remainder
 47   done # Euclid's algorithm
 48 
 49   # Last $dividend is the gcd. 
 50 } # 最後的$dividend 就是 gcd.
 52 gcd $1 $2
 53 echo; echo "GCD of $1 and $2 = $dividend"; echo
 54 # 練習:
 55 # --------
 56 # 檢查命令行參數來確定它們都是整數,
 57 #+ and exit the script with an appropriate error message if not.
 58 #+ 否則就選擇合適的錯誤消息退出.

結果:



2 使用算術操作符

vim arit.sh

  1 # !/bin/bash
  2 #  使用算術操作符
  3 
  4 n=1;echo -n "$n"
  5 
  6 let "n=$n+1" # let "n=n+1"  也可以
  7 
  8 echo -n "$n"
  9 
 10 : $((n=$n+1))
 11 
 12 # ":" 是必須的,這是因爲,如果沒有":"的話,Bash 將
 13 #+ 嘗試把"$((n = $n + 1))"解釋成一個命令
 14 
 15 echo -n "$n "
 16 (( n = n + 1 ))
 17 
 18 # 對於上邊的方法的一個更簡單的選則.
 19 # Thanks, David Lombard, for pointing this out.
 20 echo -n "$n "
 21 
 22 n=$(($n + 1))
 23 echo -n "$n "
 24 : $[ n = $n + 1 ]
 25 # ":" 是必須的,這是因爲,如果沒有":"的話,Bash 將
 26 #+ 嘗試把"$[ n = $n + 1 ]" 解釋成一個命令
 27 # 即使"n"被初始化成爲一個字符串,這句也能工作.
 28 echo -n "$n "
 29 
 30 n=$[ $n + 1 ]
 31 # 即使"n"被初始化成爲一個字符串,這句也能工作.
 32 #* Avoid this type of construct, since it is obsolete and nonportable.
 33 #* 儘量避免這種類型的結果,因爲這已經被廢棄了,並且不具可移植性.
 34 # Thanks, Stephane Chazelas.
 35 echo -n "$n "
 36 
 37 # 現在來個 C 風格的增量操作.
 38 # Thanks, Frank Wang, for pointing this out.
 39 
 40 let "n++" # let "++n" also works.
 41 echo -n "$n "
 42 
 43 (( n++ )) # (( ++n ) also works.
 44 echo -n "$n "
 45 
 46 : $(( n++ )) # : $(( ++n )) also works.
 47 echo -n "$n "
 48 
 49 : $[ n++ ] # : $[ ++n ]] also works
 50 echo -n "$n "
 51 
 52 echo
 53 
 54 exit 0

效果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章