shell-判斷循環

shell條件測試

test
每一個完整的合理的編程語言都具備條件判斷的功能、
bash能夠使用test命令,[]和()操做,還有if/then結構

字符串判斷shell

-n string 判斷字符串長度非零
-z string 判斷字符串長度爲零
[root@localhost test]# test -n zyg
[root@localhost test]# echo $?
0
[root@localhost test]# test -n ""
[root@localhost test]# echo $?
1
[root@localhost test]# test -n "x"
[root@localhost test]# echo $?
0
[root@localhost test]# test -z "zyg"
[root@localhost test]# echo $?
1
[root@localhost test]# test -z ""
[root@localhost test]# echo $?
0
[root@localhost test]# 
string1=string2 字符串相等
string1!=string2 字符串不相等
[root@localhost test]# test "root"="root"
[root@localhost test]# echo $?
0
[root@localhost test]# test "root"="Root"
[root@localhost test]# echo $?
0
[root@localhost test]# 

整數判斷編程

integer1 -eq integer2 相等
integer1 -ge integer2 大於等於
integer1 -gt integer2 大於
integer1 -le integer2 小於等於
integer1 -lt integer2 小於
integer1 -ne integer2 不等於
[root@localhost test]# test 100 -eq 100
[root@localhost test]# echo $?
0
[root@localhost test]# [ 100 -gt 90 ]
[root@localhost test]# echo $?
0
[root@localhost test]# 

文件類型判斷

-d FIle 文件存在並是一個目錄
-e File 文件存在
-f File 文件存在並是一個普通文件
-s File 文件存在並非空文件

文件權限判斷

-r File 文件存在並具備讀權限
-w File 文件存在並具備寫權限
-x File 文件存在並具備執行權限
|| 邏輯或 前邊命令失敗執行後邊的命令
&& 邏輯與 前邊命令成功後運行後邊命令

shell分支if語句

if [條件1]
    then 動做1
fi

雙分支結構

if [條件1]
    then 動做1
else
    動做2
fi

多分支結構

if [條件1]
    then 動做1
elif [條件2]
    then 動做2
…………、
else
    動做n
fi

判斷條件1是否爲真,若是爲真,執行語句1,若是爲假,判斷條件2,若條件2爲真,執行語句1.。。。若全部條件都爲假,執行語句nvim

shell分支case語句

case 變量 in
模式1)
    動做1
;;
模式2)
    動做2
;;
... ...
模式N)
    動做N
;;
*)
    動做
;;
esac

shell循環for語句

for 變量 in 值1 值2 值3 ...
do
    動做1
    動做2
    ... ...
done
for ((設置計數器;測試計數器;增長計數器))
do
    動做1
    動做2
    ... ... 
done

列表循環

selcet 變量 in 命令1 命令2 命令3 ... ...
do     
    都能作
done
生成命令列表

shell循環while語句

while 條件
do
    動做1
    動做2
    ... ...
done
當while後條件爲真的時候,就執行do和done之間的語句,知道條件爲假結束
while true
    do
     ...
done
:shell裏叫作空指令,什麼也不作

shell循環until語句

until 條件
do
    動做1
    動做2
    ... ...
done
當until後的條件wi假的時候,就執行do 和done之間的語句,知道條件爲真結束

shell循環控制continue,break,exit

break 跳出整個循環
continue 跳出當前循環,不在執行continue後面的語句,進入下一次循環
exit 會直接退出整個程序,

shell練習

?root@zyg test?# vim ./z.sh
#!/bin/bash
for ((i=1?i<=5;i++))
do
    if id user$i &> /dev/null
    then echo "user$i is exists!"
    else
    echo "create user$i..."
    useradd user$i &> /dev/null
    echo "123456" | passwd --stdin user$i &> /dev/null
    echo "user$i create seucess"
    fi
done

#!/bin/bash
for ((g=1?g<=$1;g++))
do
    for ((j=1;j<=$2;j++))
    do
        echo -n "*"
    done
    echo
done    

#!/bin/bash
for ((i=1;i<=($1+1)/2;i++))
do
    for ((n=1;n<=($1+1)/2-i;n++))
    do
        echo -n " "
    done
    for ((j=1;j<=$i*2-1;j++))
    do
        echo -n "*"
    done
    echo
done

#!/bin/bash
num=$1
for ((i=1;i<=($num+1)/2;i++))
do
    for ((k=1;k<=($num+1)/2-i+1;k++))
    do
        echo -n " "
    done
    for ((j=1;j<=i*2-1;j++))
    do
        echo -n "*"
    done
    echo
done
for ((n=1;n<=($num-1)/2;n++))
do
    for ((p=1;p<=($num-1)/2-n;p++))
    do
        echo -n " "
    done
    for ((o=1;o<=n*2+3;o++))
    do
        echo -n "*"
    done
    echo
done
for ((l=1;l<=($num-1)/2;l++))
do
    for ((m=1;m<=($num+1)/2;m++))
    do
        echo -n " "
done
echo "*"
done

#!/bin/bash
for ((g=1;g<=($1+1)/2;g++))
do
    for ((j=1;j<=($1+1)/2-g;j++))
    do
        echo -n " "
    done
    for ((i=1;i<=g*2-1;i++))
    do
        echo -n "*"
    done
    echo
done
for ((k=1;k<=($1-1)/2;k++))
do
    for ((n=1;n<=k;n++))
    do
        echo -n " "
    done
    for ((l=1;l<=$1-2*k;l++))
    do
        echo -n "*"
    done
        echo
done

#!/bin/bash
for ((i=1;i<=9;i++))
do
    for ((j=1;j<=i;j++))
    do
        echo -n "$i*$j=$?$i*$j? "
    done
    echo
done

#!/bin/bash
if [ $USER = "root" ]
then
    /etc/init.d/sshd start
else
    echo " please start sshd serivce"
fi

 

 更多請本身測試,網上案例不少,我只不過是代碼的搬運工,只是走了下這個流程bash