一、使用if-then语句

语法:

if  command

then

    commands

fi

++++++++++++++++++++++

列子:

#!/bin/bash

if  pwd

then

     echo "it worked!"

fi

这个脚本在if行采用了pwd命令。如果命令执行成功结束,echo语句就会显示该文本字符串。

二、if-then-else语句

语法:

if  command

then

    commands

else

    commands

fi

当if语句中的命令返回退出状态码0时,then部分中的命令会被执行,这跟普通的if-then语句一样。

当if语句中的命令返回非0退出状态码时,bash shell会执行else部分命令。

三、嵌套if

语法:

if  commandl

then

    commands

elif  command2

then

     more  commands

fi

四、数值比较

N1  -eq  N2    检查N1是否与N2相等

N1  -ge  N2    检查N1是否大于或等于N2

N1  -gt  N2     检查N1是否大于N2

N1  -le  N2     检查N1是否小于或等于N2

N1  -lt  N2      检查N1是否小于N2

N1  -ne  N2    检查N1是否不等于N2

shell_使用结构化命令(一)

五、字符串比较

str1  =  str2       :检查str1是否和str2相同

str1  !=  str2      :检查str1是否和str2不同

str1  <   str2      :检查str1是否小于str2

str1  >   str2      :检查str1是否大于str2

-n  str1      检查str1 的长度是否非0

-z  str1      检查str1 的长度是否为0

 

 

相关文章:

  • 2022-12-23
  • 2021-07-11
  • 2021-12-26
  • 2022-12-23
  • 2021-11-20
  • 2021-12-13
  • 2021-11-20
  • 2021-08-10
猜你喜欢
  • 2022-12-23
  • 2021-04-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-02-28
  • 2021-12-14
相关资源
相似解决方案