-a:逻辑与
    -o:逻辑或

    赋值=两边无空格,其他的 = 有空格

    -eq:等于
    -ne:不等于
    -gt:大于
    -ge:大于或等于
    -lt:小于
    -le:小于或等于

    if语句

    1. #!/bin/bash
    2. read -p "enter the number:" num
    3. if [ $num -ge 50 ] 两种写法 #if test $num -ge 50
    4. then
    5. echo "the number >= 50"
    6. else
    7. echo "the number <= 50"
    8. fi

    串测试
    test命令也可用于字符串。◆下表是用于串测试的运算符О
    image.png

    1. #!/bin/bash
    2. read -p "Do you want to continue (y/n):" choice
    3. if [ $choice = "y" -o $choice = "Y" -o $choice = "yes" ]
    4. then echo "you want to continue."
    5. elif [ $choice = "n" -o $choice = "N" -o $choice = "no" ]
    6. then echo "you want to stop."
    7. else echo "incorrect choice."
    8. fi

    文件测试
    test命令也可用于检查文件的状态·先是文件测试运算符:
    image.png

    1. #!/bin/bash
    2. read -p "enter a filename:" filename
    3. if test -e ~/$filename
    4. then echo "exist."
    5. else echo "not exist."
    6. fi
    1. #!/bin/bash
    2. read -p "enter a filename:" filename
    3. #if test -e ~/$filename
    4. #then echo "exist."
    5. #else echo "not exist."
    6. #fi
    7. [ -e ~/$filename ]$$echo "exist."||echo "not exist."

    文件测试
    嵌套if
    if[-e ]存在
    then 1、判断文件是目录
    2、elif 是一般文件
    3、列出文件详细信息
    fi
    else 任务1:创建文件
    2:列出文件的详细信息
    fi

    1. #!/bin/bash
    2. read -p "enter a filename:" name
    3. if [ -e ~/$name ]
    4. then
    5. if [ -d $name ]
    6. then "echo $name is a dir."
    7. elif [ -f $name ]
    8. then echo "$name is a ordinary file."
    9. ls -al ~/$name
    10. fi
    11. else
    12. touch ~/$name
    13. echo "$name is created."
    14. ls -al ~/$name
    15. fi

    **
    image.png
    case分支语句 实例

    1. #!/bin/bash
    2. echo "----------------------操作菜单-----------------"
    3. echo "|---1:在/home下创建cloud目录-----------------|"
    4. echo "|---2:在/home/cloud目录下创建test文件--------|"
    5. echo "|---3:删除cloud目录--------------------------|"
    6. echo "|---按其他键:重新执行此脚本文件--------------|"
    7. echo "-----------------------------------------------"
    8. read -p "Please enter anumber(1-3):" num
    9. case $num in
    10. "1")
    11. mkdir /home/cloud;;
    12. "2")
    13. touch /home/cloud/test;;
    14. "3")
    15. rm -rf /home/cloud;;
    16. *)
    17. sh menu.sh;;
    18. esac
    1. #!/bin/bash
    2. read -p "press akey, then press return:" key
    3. case $key in
    4. [a-zA-Z])
    5. echo "It's a letter."
    6. echo "$key";;
    7. [0-9])
    8. echo "It's a digt.";;
    9. *)
    10. echo "It's function key, Spacebar or other keys."
    11. esac

    循环
    image.png
    image.png
    image.pngimage.pngimage.pngimage.pngimage.pngimage.png

    1. #!/bin/bash
    2. n=1
    3. sum=0
    4. while [ $n -le 100 ]
    5. do
    6. sum=$((sum+n))
    7. n=$((n+1))
    8. done
    9. echo "1+2+...+100=$sum"
    10. n=1
    11. sum=0
    12. for n in $(seq 1 100)
    13. do
    14. sum=$((sum+n))
    15. n=$((n++))
    16. done
    17. echo "1+2+...+100=$sum"
    18. n=1
    19. sum=0
    20. for((n=1;n<=100;n++))
    21. do

    image.png

    #!/bin/bash
    usernames=$(cut -d ":" -f 1 /etc/passwd)
    for user in $usernames
    do
      id $user
    done
    

    image.png

    #!/bin/bash
    network="192.168.220"
    for host in $(seq 133 142)
    do
      ping -c 1 ${network}.${host} &> /dev/null && result=0 ||result=1
     # ping -c 1 ${network}.${host} &> /dev/null && echo "online" ||echo "offline"
    #result=1
      if [ $result = "0" ]
      then echo "${network}.${host} is online."
      else echo "${network}.${host} is offline." 
      fi
    done
    

    image.png