• Shell基本语法--流程控制


    if else 语句

    基本结构
    注意:

    1. ifelif 语句后面要跟then才能开始执行语句块
    2. then 可以换行写, 写在同一行要加分号隔开, 建议写在同一行, 结构会更清晰
    3. 没有 else if 这种语句, 只能写 elif
    if [  ]; then
       # if 语句块
    elif [  ]; then
       # 语句块
    else
       # 语句块
    fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    条件运算符 [][[]] 的不同之处

    1. [[]] 运算符只有bash中才有, 其他shell如ash, dash中是没有这个语法的
    2. [[]][]中没有的功能, 比如比较两个字符串是否相等, 且字符串中有空格的时候, 使用 [] 时必须加双引号
      if [ "$str" = "hello man" ]; then
      
      • 1
      而使用 [[]] 时就不必担心这个问题
      if [[ $str = "hello man" ]]; then
      
      • 1
    3. 逻辑运算. 使用 [] 条件运算符时, &&, ||, <, > 这几个符号都会被当作命令的一部分, 而不是当作逻辑运算符, 使用 [[]] 条件运算符时就不会有这个问题, 如:
      # 下面的 > 符号会被当作重定向命令, && 也作为命令的一部分, 会创建200 和 100两个文件, 且语句会报错: [: missing `]'
      if [ 300 > 200 && 200 > 100 ]; then
      
      # 下面的语句就可以正常的执行逻辑运算
      if [[ 300 > 200 && 200 > 100 ]]; then
      
      • 1
      • 2
      • 3
      • 4
      • 5
    4. 模糊匹配. 使用[[]] 时可以使用=~运算符及正则语法实现模糊匹配如匹配 yes :
      # 使用 [] 时只能使用如下写法
      if [ $str = "y" -o $str = "yes" ];then
      
      # 使用 [[]] 时就可以有如下写法
      if [[ $str =~ ^y(es)$ ]];then
      	echo ${BASH_REMATCH[1]}  # 匹配到的值存在这个系统变量中
      	...
      
      # 还可以使用下面的通配符写法
      if [[ $str = y* ]];then
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    5. 经验, 一般做判断时会使用 [] 条件运算符, 但在做字符串比较时, 最好用 [[]] 会降低出错概率

    条件标识符

    判断数字

    if [ $a -eq $b ]; then # 等于 equal
    if [ $a -ne $b ]; then # 不等于 not equal
    if [ $a -gt $b ]; then # 大于 greater than
    if [ $a -ge $b ]; then # 大于等于  greater equal
    if [ $a -lt $b ]; then # 小于 little than
    if [ $a -le $b ]; then # 小于等于 little equal
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    判断字符串

    # 使用 [] 运算符
    if [ -z "$str" ]; then           # 判断为空(字符串变量加引号)
    if [ -n "$str" ]; then           # 判断不为空(字符串变量加引号)
    if [ "$str" = "abc" ]; then      # 判断相等(字符串变量加引号)
    if [ "$str" == "abc" ]; then     # 判断相等(字符串变量加引号)
    if [ "$str" != "abc" ]; then     # 判断不相等(字符串变量加引号)
    if [ "$str1" \> "$str2" ]; then  # 判断大于(字符串变量加引号)
    if [ "$str1" \< "$str2" ]; then  # 判断小于
    
    # 使用 [[]] 运算符
    if [[ -z $str ]]; then           # 判断为空
    if [[ -n $str ]]; then           # 判断不为空
    if [[ $str = "ab c" ]]; then     # 判断相等
    if [[ $str == "ab c" ]]; then    # 判断相等
    if [[ $str != "ab c" ]]; then    # 判断不相等
    if [[ $str1 > $str2 ]]; then     # 判断大于
    if [[ $str1 < $str2 ]]; then     # 判断小于
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    文件判断

    if [ -e $file ]; then  # 判断文件存在
    if [ -L $file ]; then  # 判断文件存在且是链接文件(要最先判断, 否则会先去判断软链接指向的文件的其他属性)
    if [ -s $file ]; then  # 判断文件存在且是字符设备
    if [ -b $file ]; then  # 判断文件存在且是块设备文件
    if [ -d $file ]; then  # 判断文件存在且是文件夹/目录
    if [ -f $file ]; then  # 判断文件存在且是普通文件
    if [ -S $file ]; then  # 判断文件存在且是套接字文件
    if [ -p $file ]; then  # 判断文件存在且是管道文件
    # 对文件权限的判断
    if [ -r $file ]; then # 判断文件具有可读权限
    if [ -w $file ]; then # 判断文件具有可写权限
    if [ -x $file ]; then # 判断文件具有可执行权限 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    逻辑运算

    # 使用 [] 时
    if [ $a -o $b ];then 	# 或 or
    if [ $a -a $b ];then 	# 且 and 
    if [ ! $a ];then		# 非
    
    if [ expression1 ] || [ expression2 ];then	# 或
    if [ expression1 ] && [ expression2 ];then	# 且
    if ! [ expression ]							# 非
    
    # 使用 [[]]时
    if [[ $a && $b ]];then	# 或
    if [[ $a && $b ]];then  # 且
    if [[ ! $a ]];then		# 非
    
    if [[ expression1 ]] || [[ expression2 ]];then	# 或
    if [[ expression1 ]] && [[ expression2 ]];then	# 且
    if ! [[ expression ]]							# 非
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    switch case 选择

    case ${var} in
        [1-9]) # 范围
            # do something
            ;;
        [^a-zA-Z]) # 非此范围
            # do something
            ;;
        *)
            #do something
            ;;
    esac
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    select in 选择

    select var in ch_a ch_b ch_c ch_d
    do
        #经常结合case in 语句使用
    done
    
    • 1
    • 2
    • 3
    • 4

    while 循环

    while test
    do
        # 循环体
    done
    
    while true # 死循环的写法
    do
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    for 循环

    for ((i=0; i<=100; i++))
    do
        # 循环体
    done
    
    
    for var # 不加任何参数会循环所有的位置参数
    do
    
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    for in 循环

    for var in a b c d e
    do 
        #
    done
    
    for var in {1..100}
    do
        #
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    break 与 continue

    shell 中的break和continue与普通C系列程序中的同名关键词有着不同的行为
    break n : 退出n层循环
    continue n : 跳过n层循环, 执行

  • 相关阅读:
    数据结构题型16-线索二叉树
    Shiro学习与笔记
    交换机基础知识之安全配置
    剑指 Offer 10- II. 青蛙跳台阶问题【力扣】
    Diffie-Hellman协议中间人攻击方法及协议改进(网络空间安全实践与设计)
    Redis-集群
    【RocketMQ】消息的刷盘机制
    英语口语学习(2)
    音频频谱动画的原理与实现(一)
    gitee的注册和代码提交
  • 原文地址:https://blog.csdn.net/u010571709/article/details/125404945