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

效果:


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