流程控制

if判断

if基本语法

if [ 条件判断式 ];then
    程序
fi
或者
if [ 条件判断式 ]
then
        程序
fi
# 写成一行
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
# 注意事项
[ 条件判断式 ],中括号和条件判断式之间必须有空格
if后要有空格

if else语法

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

if else-if else语法

[root@slave2 testshell]# vim if.sh 
#!/bin/bash

if [ $1 -eq '1' ]
then 
        echo 'banzhang zhen shuai'
elif [ $1 -eq 2 ]
then
        echo 'cls zhen mei'
fi
~ 
[root@slave2 testshell]# bash if.sh 
if.sh: line 3: [: -eq: unary operator expected
if.sh: line 6: [: -eq: unary operator expected
[root@slave2 testshell]# bash if.sh 1
banzhang zhen shuai
[root@slave2 testshell]# bash if.sh 12
[root@slave2 testshell]# bash if.sh 2 
cls zhen mei

case语句

基本语法

  case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。case语句格式如下:

case $变量名 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
   *)
    command1
    command2
    ...
    commandN
    ;;
esac

案例实操

[root@slave2 testshell]# vim case.sh 
#!/bin/bash

case $1 in
1)
        echo 'banzhang'
;;
2)
        echo 'cls'
;;
*)
        echo 'renyao'
;;
esac
~
[root@slave2 testshell]# bash case.sh 1
banzhang
[root@slave2 testshell]# bash case.sh 2
cls
[root@slave2 testshell]# bash case.sh 3
renyao
[root@slave2 testshell]# bash case.sh 37
renyao

for循环

基本语法

# 第一种
for((初始值;循环控制条件;变量变化))
do
    程序
done

# 第二种
for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
# 写成一行
for var in item1 item2 ... itemN; do command1; command2… done;

案例实操

案例一

[root@slave2 testshell]# vim for.sh 
#!/bin/bash

s=0
for ((i=1;i<=100;i++))
do
        s=$[$s+$i]
done
echo $s
~   
"for.sh" 8L, 68C written                                                                                                            
[root@slave2 testshell]# bash for.sh 
5050

案例二

[root@slave2 testshell]# vim for2.sh 
#!/bin/bash

for i in $*
do
        echo "banzhang xihuan $i"
done
~       
[root@slave2 testshell]# bash for2.sh mm cls
banzhang xihuan mm
banzhang xihuan cls
[root@slave2 testshell]# bash for2.sh mm cls hahha
banzhang xihuan mm
banzhang xihuan cls
banzhang xihuan hahha

案例三

[root@slave2 testshell]# vim for2.sh 
#!/bin/bash

for i in $*
do
        echo "banzhang xihuan $i"
done

for i in $@
do
        echo "banzhang xihuan $i"
done
~               
"for2.sh" 11L, 115C written                                                                                                         
[root@slave2 testshell]# bash for2.sh mm cls hahha
banzhang xihuan mm
banzhang xihuan cls
banzhang xihuan hahha
banzhang xihuan mm
banzhang xihuan cls
banzhang xihuan hahha

[root@slave2 testshell]# vim for2.sh              
#!/bin/bash

for i in "$*"
do
        echo "banzhang xihuan $i"
done

for i in "$@"
do
        echo "banzhang xihuan $i"
done
"for2.sh" 11L, 119C written                                                          [root@slave2 testshell]# bash for2.sh mm cls hahha
banzhang xihuan mm cls hahha
banzhang xihuan mm
banzhang xihuan cls
banzhang xihuan hahha

while循环

基本语法

while [ 条件表达式 ]
do
    程序
done

案例实操

[root@slave2 testshell]# vim while.sh 
#!/bin/bash

i=1
s=0
while [ $i -le 100 ]
do
        s=$[$s+$i]
        i=$[$i+1]
done
echo $s
~       
"while.sh" 10L, 81C written                                                                                                         
[root@slave2 testshell]# bash while.sh 
5050

read读取控制台输入

基本语法

read(选项)(参数)
选项:
    -p:指定读取值时的提示符
    -t:指定读取值等待的时间(秒)
参数:
    变量:指定读取值的变量名

实例操作

[root@slave2 testshell]# vim read.sh 
#!/bin/bash

read -t 10 -p "input your name " NAME
echo $NAME
"read.sh" 4L, 62C written                                                                                                           
[root@slave2 testshell]# bash read.sh 
input your name 
[root@slave2 testshell]# bash read.sh 
input your name topic
topic
posted on 2019-02-22 19:50  0x153_小波  阅读(120)  评论(0编辑  收藏  举报