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

 

 更多请自己测试,网上案例很多,我只不过是代码的搬运工,只是走了下这个流程

 

posted @ 2018-09-03 15:58  前方、有光  阅读(2690)  评论(0编辑  收藏  举报