您的位置:首页 > 博客中心 > 网络系统 >

bash/shell编程学习(2)

时间:2022-04-03 11:14

先来复习重定向的用法:

1.快速清空文件

cat demo.txt < /dev/null

注:linux中有一个经典名言【一切皆文件】,/dev/null可以认为是一个特殊的空文件,更形象点,可以理解为科幻片中的黑洞,任何信息重向定输出到它后,便有去无回,当然黑洞里也没有信息能出来。

综合来讲,上面的意思就是利用<将黑洞做为demo.txt的标准输入,黑洞里没任何内容,任何文件里的内容被它吞噬了,自然也没就没东西能剩下了,所以最终就是demo.txt被黑洞洗空了。

/dev/null 还有其它用法,比如用它可以让nohup不生成nohup.out文件,见: 

 

2.执行时输出源码

#!/bin/bash -v
printf ‘%0.2f\n‘ 12.12334

执行结果如下:

#!/bin/bash -v
printf ‘%0.2f\n‘ 12.12334
12.12

注意:第3行输出结果之前,把源码也打印出来了,秘密在于第1行最后的 -v 参数 

 

3.调试模式

#!/bin/bash -x
printf ‘%0.2f\n‘ 12.12334
echo ‘hello‘

执行结果如下:

+ printf ‘%0.2f\n‘ 12.12334
12.12
+ echo hello
hello

注意:第一行后面的参数变成了-x,加上这个后,执行时,每一行代码在执行前,会先输出对应的源码,并且以+开头,十分方便调试。  

 

4. if与test及[]

4.1 数字判断

#!/bin/bash -x
i=$1 #变量i的值取第1个参数的值
if test $i -gt 89; then #如果i>89
  echo ‘A‘
elif test $i -gt 79; then #如果i>79
  echo ‘B‘
elif test $i -eq 60 -o $i -gt 60;then #如果i=60或i>60(即:i>=60)
  echo ‘C‘
elif test $i -gt 0;then #如果i>0
  echo ‘D‘
elif test $i -lt 0;then #如果i<0
  echo ‘invalid‘
else #i==0的情况
  echo ‘zero‘ 
fi

注:if test 条件; then 语句 fi 这是基本格式,注意条件后的;不可省略,另外结束符号是fi(即:把if倒过来,有点回文的理念),另外要记住一堆缩写

-lt 即-Less Than的缩写,表示小于

-gt 即-Greater Than的缩写,表示大于

-eq 即-equal的缩写,表示等于,此外还有

-ne 即-Not Equal的缩写,表示不等于

-o 即-or,表示前后二个逻辑判断是『或』的关系,类似的

-a 即-and,表示前后二个逻辑判断是『与』的关系

elif 即else if的缩写 

上面的示例运行结果:

./demo.sh 90
+ i=90
+ test 90 -gt 89
+ echo A
A

test语句还有一个简化的写法,即把"test 条件"变成" [ 条件 ] ",注意二端的方括号左右都要加一个空格,所以上面的写法可以改成:

i=$1
if [ $i -gt 89 ]; then
  echo ‘A‘
elif [ $i -gt 79 ]; then
  echo ‘B‘
elif [ $i -eq 60 -o $i -gt 60 ]; then
  echo ‘C‘
elif [ $i -gt 0 ]; then
  echo ‘D‘
elif [ $i -lt 0 ]; then
  echo ‘invalid‘
else
  echo ‘zero‘
fi

这样看起来就美观多了,如果不喜欢-o这种逻辑或的写法,第6行也可以换成这样

elif [ $i -eq 60 ] || [ $i -gt 60 ]; then

但是执行的细节略有区别,在调试模式下可以对比下,用||写法的输入(测试用例:61)

./demo2.sh 61
+ i=61
+ ‘[‘ 61 -gt 89 ‘]‘
+ ‘[‘ 61 -gt 79 ‘]‘
+ ‘[‘ 61 -eq 60 ‘]‘
+ ‘[‘ 61 -gt 60 ‘]‘
+ echo C
C

而用-o写法的输出:

./demo2.sh 61
+ i=61
+ ‘[‘ 61 -gt 89 ‘]‘
+ ‘[‘ 61 -gt 79 ‘]‘
+ ‘[‘ 61 -eq 60 -o 61 -gt 60 ‘]‘
+ echo C
C

对比下5-6行可以发现,区别在于判断一次,还是判断二次 

4.2 字符串判断

#!/bin/bash -x
str1="abc"
if [ -z "$str1" ]; then
  echo ‘str1 is empty‘
else
  echo ‘str1 is not empty‘
fi

printf "\n"

str2=""
if [ -n "$str2" ]; then
  echo ‘str2 is not empty‘
else
  echo ‘str2 is empty‘
fi

printf "\n"

if [ "$str1" = "$str2" ]; then
  echo ‘str1 = str2‘
else
  echo ‘str1 <> str2‘
fi

注: -n即-not empty判断字符串非空,-z即-zero判断字符串为空,=判断字符串相同(判断字符串时,记得要加双引号)

运行结果:

+ str1=abc
+ ‘[‘ -z abc ‘]‘
+ echo ‘str1 is not empty‘
str1 is not empty
+ printf ‘\n‘

+ str2=
+ ‘[‘ -n ‘‘ ‘]‘
+ echo ‘str2 is empty‘
str2 is empty
+ printf ‘\n‘

+ ‘[‘ abc = ‘‘ ‘]‘
+ echo ‘str1 <> str2‘
str1 <> str2

4.3 文件及目录判断

#!/bin/bash -x
if [ -f ~/.bash_profile ]; then
  echo ‘~/.bash_profile is a file‘
else
  echo ‘~/.bash_profile is not a file‘
fi

printf ‘\n‘

if [ -d ~/ ]; then
  echo ‘~/ is a directory‘
else
  echo ‘~/ is not a directory‘
fi

-f即判断是否为file, -d即判断是否为directory, 输出结果:

+ ‘[‘ -f /Users/yjmyzz/.bash_profile ‘]‘
+ echo ‘~/.bash_profile is a file‘
~/.bash_profile is a file
+ printf ‘\n‘

+ ‘[‘ -d /Users/yjmyzz/ ‘]‘
+ echo ‘~/ is a directory‘
~/ is a directory

  

  

  

热门排行

今日推荐

热门手游