if [ "$num" -gt "150" ];then echo "$num is biger than 150" fi ################### if [ "$var1" = "$var2" ]; then echo '$var1 eq $var2' else echo '$var1 not eq $var2' fi
if [ -f file ] then echo"yes yes yes" elif [ -z file ] then echo"yes yes" else echo"nonono" fi
复杂逻辑判断
1 2 3 4
复杂逻辑判断 -a 与 -o 或 ! 非
实例
1 2 3
(( a > b )) || (( a < c )) [[ $a > $b ]] || [[ $a < $c ]] [ $a -gt $b -o $a -lt $c ]
读取用户输入
1 2 3
#!/bin/bash read -p "Please Enter You Name: " NAME echo"Your Name Is: $NAME"
for 循环
1 2 3 4 5 6
#!/bin/bash COLORS="red green blue" for COLOR in$COLORS do echo"The Color is: ${COLOR}" done
在当前所有txt文件前面追加new实现重命名
1 2 3 4 5 6 7 8
#!/bin/bash FILES=$(ls *txt) NEW="new" for FILE in$FILES do echo"Renaming $FILE to new-$FILE" mv $FILE$NEW-$FILE done
while 循环
当所给的条件为true时,循环执行while里面的代码块
1 2 3 4 5 6 7 8 9
while [ CONNDITION_IS_TRUE ] do # Commands will change he entry condition command 1 command 2 ... ... command N done
实例
1 2 3 4 5 6 7 8
#!/bin/bash LINE=1 whileread CURRENT_LINE do echo"${LINE}: $CURRENT_LINE" ((LINE++)) done < /etc/passwd # This script loops through the file /etc/passwd line by line