流程控制语句

单分支if

 1  2  3  4  5  6  7  8  9 10 11
vim mkcdrom.sh #!/bin/bash DIR="/media/cdrom" if [ ! -e $DIR ] then mkdir -p $DIR fi bash mkcdrom.sh ls -d /media/cdrom /media/cdrom

双分支if

-c ping次数

-i 发送间隔

-W 等待超时时间

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
vim chkhost.sh #!/bin/bash ping -c 3 -i 0.2 -W 3 $1 &> /dev/null if [ $? -eq 0 ] then echo "Host $1 is On-line." else echo "Host $1 is Off-line." fi bash chkhost.sh 192.168.10.10 Host 192.168.10.10 is On-line. bash chkhost.sh 192.168.10.20 Host 192.168.10.20 is Off-line.

多分支if

-p 提示

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
vim chkscore.sh #!/bin/bash read -p "Enter your score(0-100):" GRADE if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ] ; then echo "$GRADE is Excellent" elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ; then echo "$GRADE is Pass" else echo "$GRADE is Fail" fi bash chkscore.sh Enter your score(0-100):88 88 is Excellent bash chkscore.sh Enter your score(0-100):80 80 is Pass bash chkscore.sh Enter your score(0-100):30 30 is Fail bash chkscore.sh Enter your score(0-100):200 200 is Fail

for循环语句

id 查看用户信息

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
vim Example.sh #!/bin/bash read -p "Enter The Users Password : " PASSWD for UNAME in `cat users.txt` do id $UNAME &> /dev/null if [ $? -eq 0 ] then echo "Already exists" else useradd $UNAME &> /dev/null echo "$PASSWD" | passwd --stdin $UNAME &> /dev/null if [ $? -eq 0 ] then echo "$UNAME , Create success" else echo "$UNAME , Create failure" fi fi done bash Example.sh Enter The Users Password : linuxprobe andy , Create success barry , Create success carl , Create success duke , Create success eric , Create success george , Create success tail -6 /etc/passwd andy:x:1001:1001::/home/andy:/bin/bash barry:x:1002:1002::/home/barry:/bin/bash carl:x:1003:1003::/home/carl:/bin/bash duke:x:1004:1004::/home/duke:/bin/bash eric:x:1005:1005::/home/eric:/bin/bash george:x:1006:1006::/home/george:/bin/bash vim CheckHosts.sh #!/bin/bash HLIST=$(cat ~/ipadds.txt) for IP in $HLIST do ping -c 3 -i 0.2 -W 3 $IP &> /dev/null if [ $? -eq 0 ] ; then echo "Host $IP is On-line." else echo "Host $IP is Off-line." fi done ./CheckHosts.sh Host 192.168.10.10 is On-line. Host 192.168.10.11 is Off-line. Host 192.168.10.12 is Off-line.

while循环语句

$RANDOM 随机数(范围0~32767)

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
vim Guess.sh #!/bin/bash PRICE=$(expr $RANDOM % 1000) TIMES=0 echo "商品实际价格为0-999之间,猜猜看是多少?" while true do read -p "请输入您猜测的价格数目:" INT let TIMES++ if [ $INT -eq $PRICE ] ; then echo "恭喜您答对了,实际价格是 $PRICE" echo "您总共猜测了 $TIMES 次" exit 0 elif [ $INT -gt $PRICE ] ; then echo "太高了!" else echo "太低了!" fi done bash Guess.sh 商品实际价格为0-999之间,猜猜看是多少? 请输入您猜测的价格数目:500 太低了! 请输入您猜测的价格数目:800 恭喜您答对了,实际价格是 800 您总共猜测了 2

case条件语句

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
vim Checkkeys.sh #!/bin/bash read -p "请输入一个字符,并按Enter键确认:" KEY case "$KEY" in [a-z]|[A-Z]) echo "您输入的是 字母。" ;; [0-9]) echo "您输入的是 数字。" ;; *) echo "您输入的是 空格、功能键或其他控制字符。" esac bash Checkkeys.sh 请输入一个字符,并按Enter键确认:6 您输入的是 数字。 bash Checkkeys.sh 请输入一个字符,并按Enter键确认:p 您输入的是 字母。 bash Checkkeys.sh 请输入一个字符,并按Enter键确认:^[[15~ 您输入的是 空格、功能键或其他控制字符

浙ICP备11005866号-8