Shell 脚本学习 2018-05-28

1, 几个等效命令 test,/usr/bin/test,[],和/usr/bin/[

vim equi.sh

 1 #!/bin/bash 
  2 echo
  3 
  4 if test -z "$1"
  5 then
  6    echo "No command-line arguments."
  7 else
  8    echo "First command-line argument is $1." 
  9 fi
 10 
 11 echo
 12 
 13 if /usr/bin/test -z "$1" # 与内建的 test 结果相同
 14 then
 15   echo "No command-line arguments."
 16 else
 17   echo "First command-line argument is $1." 
 18 fi
 19 
 20 echo
 21 
 22 if [ -z "$1" ] # 与上边代码的作用相同
 23 # if [ -z "$1" 应该工作,但是...
 24 #+ Bash 相应一个缺少关闭中括号的错误消息.
 25 then
 26 echo "No command-line arguments."
 27 else
 28 echo "First command-line argument is $1."
 29 fi
 30 
 31 echo

效果:

First command-line argument is hhh.


First command-line argument is hhh.


First command-line argument is hhh.


First command-line argument is hhh.

2,算数测试使用(( ))

vim count.sh

  1 #!/bin/bash
  2 # 算数测试
  3 
  4 # The (( ... )) construct evaluates and tests numerical expressions.
  5 # (( ... ))结构计算并测试算数表达式的结果.
  6 # 退出码将与[ ... ]结构相反!
  7 
  8 (( 0 ))
  9 echo "Exit status of \"(( 0 ))\" is $?." # 1
 10 
 11 (( 1 ))
 12 echo "Exit status of \"(( 1 ))\" is $?." # 0
 13 
 14 (( 5 > 4 )) # true
 15 echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0
 16 
 17 (( 5 > 9 )) # false
 18 echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1
 19 
 20 (( 5 - 5 )) # 0
 21 echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1 
 22 
 23 (( 5 / 4 )) # 除法也行
 24 echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0
 25 
 26 (( 1 / 2 )) # 出发结果<1
 27 echo "Exit status of \"(( 1 / 2 ))\" is $?." # 结果将为 0
 28 # 1
 29 
 30 (( 1 / 0 )) 2>/dev/null # 除数为 0 的错误
 31 # ^^^^^^^^^^^
 32 echo "Exit status of \"(( 1 / 0 ))\" is $?." # 1
 33 
 34 # What effect does the "2>/dev/null" have?
 35 # "2>/dev/null"的作用是什么?
 36 # 如果删除"2>dev/null"将会发生什么?
 37 # Try removing it, then rerunning the script.
 38 # 尝试删除它,然后再运行脚本.
 39 exit 0

效果:

Exit status of "(( 0 ))" is 1.
Exit status of "(( 1 ))" is 0.
Exit status of "(( 5 > 4 ))" is 0.
Exit status of "(( 5 > 9 ))" is 1.
Exit status of "(( 5 - 5 ))" is 1.
Exit status of "(( 5 / 4 ))" is 0.
Exit status of "(( 1 / 2 ))" is 1.
Exit status of "(( 1 / 0 ))" is 1.

3 test 死的链接文件

vim broken-link.sh

4,数字和字符串比较

vim numStrTest.sh

  1 #!/bin/bash
  2 
  3 a=4
  4 b=5
  5 
  6 # 这里的变量 a 和 b 既可以当作整型也可以当作是字符串.
  7 # 这里在算术比较和字符串比较之间有些混淆,
  8 #+ 因为 Bash 变量并不是强类型的.
  9 
 10 # Bash 允许对整型变量操作和比较
 11 #+ 当然变量中只包含数字字符.
 12 # 但是还是要考虑清楚再做.
 13 
 14 echo
 15 
 16 if [ "$a" -ne "$b" ]
 17 then
 18   echo "$a is not equal to $b"
 19   echo "(arithmetic comparison)"
 20 fi
 21 echo
 22 
 23 if [ "$a" != "$b" ]
 24 then
 25    echo "$a is not equal to $b."
 26   echo "(string comparison)"
 27 # "4" != "5"
 28 # ASCII 52 != ASCII 53
 29 fi
 30 # 在这个特定的例子中,"-ne"和"!="都可以.
 31 echo
 32 exit 0




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值