标签:shell脚本 for循环 while循序 until循环
for循环
for 变量名in 列表;do
循环体
done
执行机制:
依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束
列表生成方式:
(1) 直接给出列表
(2) 整数列表:
(a) {start..end}
(b) $(seq[start [step]] end)
(3) 返回列表的命令
$(COMMAND)
(4) 使用glob,如:*.sh
(5) 变量引用;
$@, $*
1、判断/var/目录下所有文件的类型
①
#!/bin/bash for filename in /var/* ;do if [ -L "$filename" ] ;then echo "$filename is link file" elif [ -f "$filename" ];then echo "$filename is common file" elif [ -b "$filename" ];then echo "$filename is block file" elif [ -c "$filename" ];then echo "$filename is char file" elif [ -S "$filename" ] ;then echo "$filename is socket file " elif [ -d "$filename" ] ;then echo "$filename is directory" else echo "Unknow" fi done
②
#!/bin/bash for f1 in /var/* ; do ff=`ls -ld $f1 | cut -c1` case $ff in l) echo "$f1 is link file" ;; b) echo "$f1 is blcok file " ;; c) echo "$f1 is char file" ;; s) echo "$f1 is socket file" ;; -) echo "$f1 is file" ;; d) echo "$f1 is dir" ;; *) echo "$f1 is other " esac done
2、添加10个用户user1-user10,密码同用户名
#!/bin/bash #添加10个用户user1-user10,密码同用户名 for i in {1..10};do id user$i &>/dev/null if [ $? -eq 0 ];then echo "user$i is exist" else useradd user$i echo "user$i" | passwd --stdin user$i &> /dev/null echo "Add user$i finished " fi done
②
删除上述用户
#!/bin/bash for i in {1..10} ; do id user$i &> /dev/null if [ $? -eq 0 ];then userdel -r user$i echo "user$i is delete" else echo "user$i is not exist" fi done
3、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的文件输出为文件加stop,以S开头的文件输出为文件名加start;
#!/bin/bash #/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的文件输出为文件加stop,以S开头的文件输出为文件名加start; for filename in /etc/rc.d/rc3.d/* ;do ff=`basename "$filename" | cut -c1` case $ff in [Kk]) echo "$filename stop" ;; [Ss]) echo "$filename start" ;; *) echo "other" esac done
4、写一个脚本,提示输入正整数n的值,计算1+2+3+…n的总和
#!/bin/bash read -p "please a number: " n1 if [[ $n1 =~ ^-?[[:digit:]]+$ ]];then if [ $n1 -gt 0 ];then sum=0 for i in `seq $n1` ;do sum=$[$sum+$i] done echo "sum is $sum" else echo "$n1 is not positive integer " fi else echo "need a number" fi
5、写一个脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
#!/bin/bash read -p "Please input a IP :" ip i=`echo $ip|cut -d. -f1-3`. if [[ $ip =~ ^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]] ;then for j in {1..255} ;do ping -c1 -W1 $i$j &> /dev/null && echo "$i$j is on " || echo "$i$j is off " done else echo "this is not IP" fi ~
6、打印九九乘法表
#!/bin/bash i=1 for i in {1..9};do for j in `seq $i` ;do echo -ne "$j*$i=$[$i*$j]\t" done echo done
while循环
while CONDITION; do
循环体
done
CONDITION:循环控制条件;进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”,则执行一次循环;直到条件测试状态为“false”终止循环
因此:CONDTION一般应该有循环控制变量;而此变量的值会在循环体不断地被修正
进入条件:CONDITION为true;
退出条件:CONDITION为false
1、求100以内所有正整数之和
#!/bin/bash i=1 sum=0 while [ $i -le 100 ];do sum=$[$sum+$i] let i++ done echo "sum is $sum"
2、通过ping命令探测172.16.250.1-254范围内的所有主机的在线状态,统计在线主机和离线主机各多少.
#!/bin/bash read -p "Please input IP " ip i=`echo $ip | cut -d. -f1-3`. j=0 if echo $ip | egrep "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$" ;then while [ $j -le 255 ] ;do ping -c1 -W1 $i$j &> /dev/null && echo "$i$j is on" || echo "$i$j is off" let j++ done else echo "This isn‘t IP" fi
3、打印九九乘法表
#!/bin/bash i=1 while [ $i -lt 10 ] ;do j=1 while [ $j -le $i ] ;do echo -ne "$i*$j=$[$i*$j]\t" let j++ done echo let i++ done
4、利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大者和最小者
#!/bin/bash i=1 a=$RANDOM max=$a min=$a while [ $i -le 10 ];do [ $max -le $a ] && max=$a [ $min -ge $a ] && min=$a echo "$a" a=$RANDOM let i++ done echo "max number is $max" echo "min number is $min" ~
5、打印国际象棋棋盘
#!/bin/bash i=1 while [ $i -le 8 ];do j=1 while [ $j -le 8 ];do sum=$[$i+$j] n=$[$sum%2] if [ $n -eq 0 ];then echo -ne "\033[41;1m \033[0m" else echo -ne "\033[43;1m \033[0m" fi let j++ done let i++ echo done
until循环
until CONDITION; do
循环体
done
进入条件:CONDITION 为false
退出条件:CONDITION 为true
1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。
#!/bin/bash until who | grep -q "^hacker\>" ; do sleep 3 done who| grep "^hacker\>"| tr -s " " | cut -d " " -f3-5 >> /var/log/login.log echo "you should logout" | mail hacker echo "hacker is login"
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出。
#!/bin/bash read -p "Guess number! Please enter a numer{0-10}: " n if [[ $n =~ ^[[:digit:]]+$ ]] ;then i=$[$RANDOM%11] until [ $n -eq $i ] ; do if [ $n -gt $i ] ; then echo "It‘s too large " else echo "It‘s too small " fi read -p "Try again:" n done echo "you are right!!!" else echo "please input a number !!!" fi
1、写个脚本:打印等腰三角形
#!/bin/bash read -p "please input a line number " n if [[ "$n" =~ ^[[:digit:]]+$ ]] ;then for i in `seq $n` ;do for j in `seq $[$n-$i]`;do echo -n " " done for k in `seq $[$i*2-1]`;do echo -n "*" done echo let i++ done else echo "need a number!" exit 2 fi
2、用until循环实现国际象棋棋盘
#!/bin/bash i=1 red="\033[41;1m \033[0m" yellow="\033[43;1m \033[0m" until [ $i -gt 8 ] ;do j=1 until [ $j -gt 8 ] ;do sum=$[$i+$j] z=$[$sum%2] if [ $z -eq 0 ] ;then echo -ne "$red" else echo -ne "$yellow" fi let j++ done let i++ echo done
本文出自 “I'm Groot” 博客,请务必保留此出处http://groot.blog.51cto.com/11448219/1840068
标签:shell脚本 for循环 while循序 until循环
原文地址:http://groot.blog.51cto.com/11448219/1840068