shell-判斷循環


shell條件測試

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

字符串判斷

-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.。。。若所有條件都為假,執行語句n

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

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM