目录
一、什么是结构化命令?
许多程序要求对shell脚本中的命令施加一些逻辑流程控制。有一类命令会根据条件使脚本跳过某些命令。这样的命令通常称为结构化命令。
二、使用逻辑化命令的好处
恰当的使用结构化命令,会使我们的代码看起来更有逻辑性、可读性。会让我们的工作变得更加轻松。
三、逻辑化命令if、for、while、until的格式
1、if格式:
if condition ;then
commands1
else
condition2
fi
注:类似于其它语言,else可要可不要。
if condition1 ;then
commands1
elif condition2;then
condition2
......
elif conditionN;then
conditionN
else
commandsN+1
fi
2、for格式
for n in conditions;do
commands
done
3、while格式
while condition(true);do
commands
done
4、until格式
until condition(false);do
commands
done
5、case格式
case condition in
condition1) command1;;
condition2) command2;;
condition3) command3;;
*)echo "error";;
四、实例
作业:
1、判断/var/目录下所有文件的类型
/app/var.sh
#!/bin/bash
cd /var
S=`ls /var`
for n in $S;do
if [ -f "$n" ];then
echo "$n is 普通文件";
elif [ -b "$n" ];then
echo "$n is 块设备文件";
elif [ -c "$n" ];then
echo "$n is 字符设备文件";
elif [ -h "$n" ];then
echo "$n is 符号链接文件"
elif [ -d "$n" ];then
echo "$n is 目录文件";
elif [ -p "$n" ];then
echo "$n is 管道文件";
elif [ -s "$n" ];then
echo "$n is 套接字文件";
fi
done
2、添加10个用户user1-user10,密码为8位随机字符
tr -cd '[[:alnum:].@#!_]' < /dev/urandom |head -c 8
/zuoye/uadd.sh
#!/bin/bash
for n in `echo user{1..10}`;do
useradd $n
S=`tr -cd '[[:alnum:].@#!_]' < /dev/urandom |head -c 8`
echo $S|passwd --stdin $n &> /dev/null && echo "$n is created"
done
删除用户:/zuoye/udel.sh
#!/bin/bash
for n in `echo user{1..10}`;do
userdel -r $n && echo "$n is delete!"
done
3、centos6下 /etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如
K34filename stop
S66filename start
/zuoye/ks.sh
#!/bin/bash
cd /etc/rc.d/rc3.d
s=`ls`
for n in $s;do
d=`echo $n|cut -c 1`
if [ $d == K ];then
echo $n stop
elif [ $d == S ];then
echo $n start
fi
done
4、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
/zuoye/sum.sh
#!/bin/bash
read -p "Please input number:" Num
Sum=0
if [ $Num -gt 0 ];then
for n in $(seq 1 $Num);do
Sum=$[$Sum+$n]
done
echo "1+2+...+$Num = $Sum"
fi
5、计算100以内所有能被3整除的整数之和
/zuoye/dt.sh
一、echo {3..100..3}|tr ' ' '+'|bc
二、Sum=0
for n in $(seq 1 100);do
if [ $[$n%3] -eq 0 ];then
Sum=$[$Sum+$n]
fi
done
echo $Sum
6、打印九九乘法表 用for写
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6
/zuoye/jiufor.sh
#!/bin/bash
for n in $(seq 1 9);do
for i in $(seq 1 $n);do
if [ $n -eq 3 -a $i -eq 2 ];then
echo -n "$i*$n=$[$i*$n] "
elif [ $n -eq 4 -a $i -eq 2 ];then
echo -n "$i*$n=$[$i*$n] "
else
echo -n "$i*$n=$[$i*$n] "
fi
done
echo
done
7、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
/zuoye/html.sh
#!/bin/bash
S=`tr -cd [[:upper:][:lower:]] < /dev/urandom |head -c 8`
for n in $(seq 1 10);do
touch $n$S.html && echo "$n$S.html create success!"
done
8、打印等腰三角形
/zuoye/sjx.sh
#!/bin/bash
read -p "Please input number:" Hei Len
for n in $(seq 1 $Len);do
for i in $(seq 1 $Hei);do
if [ $i -ge $n ];then
if [ `echo $[$Len-$n]` -lt $i ];then
echo -n "?"
fi
fi
done
echo
done
9、编写脚本,求100以内所有正奇数之和
echo {1..100..2}|tr ' ' '+'|bc
Sum=0
for n in $(seq 1 100);do
if [ $[$n%2] -eq 1 ];then
Sum=$[$Sum+$n]
fi
done
echo $Sum
10、编写脚本,打印九九乘法表 用while写
/zuoye/jiuwhile.sh
#!/bin/bash
var1=1
var2=1
while [ $var1 -lt 10 ];do
while [ $var2 -le $var1 ];do
echo -n "$var2*$var1=$[$var1*$var2] "
((var2++))
done
((var1++))
var2=1
echo
done
11、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值
/zuoye/random.sh
#!/bin/bash
read -p "Please input num:" A
T=0
S=101
for n in $(seq 1 $A);do
U=`echo $[$RANDOM%100+1]`
echo $U
if [ $U -gt $T ];then
T=$U
fi
if [ $U -lt $S ];then
S=$U
fi
done
echo "Maxnumis $T"
echo "minnumis $S"
#!/bin/bash
A1=`echo $[$RANDOM%100+1]`
A2=`echo $[$RANDOM%100+1]`
A3=`echo $[$RANDOM%100+1]`
A4=`echo $[$RANDOM%100+1]`
A5=`echo $[$RANDOM%100+1]`
A6=`echo $[$RANDOM%100+1]`
A7=`echo $[$RANDOM%100+1]`
A8=`echo $[$RANDOM%100+1]`
A9=`echo $[$RANDOM%100+1]`
A10=`echo $[$RANDOM%100+1]`
T=0
S=$A1
for n in `echo $A{1..10}`;do
echo -n "$n "
echo
if [ $n -gt $T ];then
T=$n
fi
if [ $n -lt $S ];then
S=$n
fi
done
echo "Maxnumis $T"
echo "minnumis $S"
12、编写脚本,实现打印国际象棋棋盘
/zuoye/gjxq.sh
S=`echo -e "\e[44m \e[0m"`
D=`echo -e "\e[41m \e[0m"`
for i in $(seq 1 8);do
for j in $(seq 1 8);do
if [ $[$j%2] -eq 1 -a $[$i%2] -eq 1 ];then
echo -n $S$S
elif [ $[$j%2] -eq 0 -a $[$i%2] -eq 0 ];then
echo -n $S$S
else
echo -n $D$D
fi
done
echo
done
13、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令:
echo $RANDOM|md5sum|cut –c1-10
./md5.sh 44b2395c46 efbaf275cd 4be9c40b8b f8c8873ce0 b902c16c8b ad865d2f63
/zuoye/md5.sh
#!/bin/bash
Count=0
while [ $# -gt 0 ];do
if [ $# -gt 0 ];then
D=$RANDOM
S=`echo $D|md5sum|cut -c1-10`
if [ $S == $1 ];then
echo "String is $S"
echo "The num is $D"
echo "Times is $Count cycles!"
shift;
fi
let Count++
fi
done
后的结果,请破解这些字符串对应的RANDOM值
14、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出7
/zuoye/sjs.sh
#!/bin/bash
A=`echo $[$RANDOM%10+1]`
echo $A
P=0
until [ $P -eq $A ];do
read -p "请输入一个数字:" P
if [ $P -lt $A ];then
echo "小了"
fi
if [ $P -gt $A ];then
echo "大了"
fi
done
echo "恭喜你,猜对了!"
15、用文件名做为参数,统计所有参数文件的总行数
/zuoye/totalline.sh
#!/bin/bash
#read -p "Please input the file name:"
S=0
while [ $# -gt 0 ];do
D=`cat $1|wc -l`
S=$[$S+$D]
echo "$1 is $D lines!"
shift
done
echo "All lines is $S"
16、用二个以上的数字为参数,显示其中的最大值和最小值
/zuoye/allline.sh
#!/bin/bash
S=$1
D=$1
while [ $# -gt 0 ];do
if [ $1 -lt $D ];then
D=$1
fi
if [ $1 -gt $S ];then
S=$1
fi
shift
done
echo "maxnum is $S"
echo "minnum is $D"