13.1-Shell脚本进阶之for循环

1.判断/var/目录下所有文件的类型

[root@localhost /data/scripts]#bash for_var.sh 
Please input directory: /var
file /var/adm is Directory
file /var/cache is Directory
file /var/crash is Directory
file /var/db is Directory
file /var/empty is Directory
file /var/games is Directory
file /var/gopher is Directory
file /var/kerberos is Directory
file /var/lib is Directory
file /var/local is Directory
file /var/lock is Link
file /var/log is Directory
file /var/mail is Link
file /var/nis is Directory
file /var/opt is Directory
file /var/preserve is Directory
file /var/run is Link
file /var/spool is Directory
file /var/tmp is Directory
file /var/yp is Directory
[root@localhost /data/scripts]#cat for_var.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-25
#FileName: for_var.sh
#********************************************************************
read -p "Please input directory: " DIR
for FILE in `ls $DIR` ;do
	TY=`file $DIR/$FILE | egrep -o "link|text|block|directory"`
	case $TY in
	text)
		echo "file $DIR/$FILE is file"
		;;
	link)
		echo "file $DIR/$FILE is Link"
		;;
	block)
		echo "file $DIR/$FILE is Block"
		;;
	directory)
		echo "file $DIR/$FILE is Directory"
		;;
	*)
		echo "file $DIR/$FILE is Others"
	esac;
done

2.添加10个用户user1-user10,密码为8位随机字符.

[root@localhost /data/scripts]#bash for_useradd.sh 
user1 created successful
user2 created successful
user3 created successful
user4 created successful
user5 created successful
user6 created successful
user7 created successful
user8 created successful
user9 created successful
user10 created successful
[root@localhost /data/scripts]#cat /etc/passwd
user1:x:2003:2003::/home/user1:/bin/bash
user2:x:2004:2004::/home/user2:/bin/bash
user3:x:2005:2005::/home/user3:/bin/bash
user4:x:2006:2006::/home/user4:/bin/bash
user5:x:2007:2007::/home/user5:/bin/bash
user6:x:2008:2008::/home/user6:/bin/bash
user7:x:2009:2009::/home/user7:/bin/bash
user8:x:2010:2010::/home/user8:/bin/bash
user9:x:2011:2011::/home/user9:/bin/bash
user10:x:2012:2012::/home/user10:/bin/bash
[root@localhost /data/scripts]#cat for_useradd.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-25
#FileName: for_useradd.sh
#********************************************************************
for USER in user{1..10};do
	useradd $USER
	echo openssl rand 4 -base64 | passwd --stdin $USER &>/dev/null
	echo $USER created successful
done

3./etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start

[root@localhost /data/scripts]#bash for_rc3.sh 
K50netconsole stop
S10network start
[root@localhost /data/scripts]#cat for_rc3.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-25
#FileName: for_rc3.sh
#********************************************************************
for FILE in `ls /etc/rc.d/rc3.d/ | egrep '^K|^S'` ;do
	S=`echo $FILE | egrep -o '^K|^S'`
	if [ "$S" = "K" ];then
		echo $FILE stop
	else
		echo $FILE start
	fi;
done

4.编写脚本,提示输入正整数n的值,计算1+2+…+n的总和

[root@localhost /data/scripts]#bash for_sum.sh
Please enter a positive integer: -1
Please input digit!
[root@localhost /data/scripts]#bash for_sum.sh
Please enter a positive integer: 100
5050
[root@localhost /data/scripts]#cat for_sum.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-27
#FileName: for_sum.sh
#********************************************************************
read -p "Please enter a positive integer: " num
if [ $num -gt 0 ];then
	sum=0
	for i in `seq $num`;do
		let sum+=$i;
	done
	echo $sum
else
	echo "Please input digit!"
fi

5.计算100以内所有能被3整除的整数之和

[root@localhost /data/scripts]#bash for_divide.sh
1683
[root@localhost /data/scripts]#cat !*
cat for_divide.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-27
#FileName: for_divide.sh
#********************************************************************
sum=0
for i in {3..100..3};do
	let sum+=$i
done
echo $sum

6.编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态

[root@localhost /data/scripts]#vim for_scan_host.sh
[root@localhost /data/scripts]#bash !*
bash for_scan_host.sh
192.168.0.40 is down
192.168.0.75 is down
192.168.0.61 is down
192.168.0.56 is down
192.168.0.43 is down
192.168.0.44 is down
192.168.0.71 is down
192.168.0.35 is down
192.168.0.38 is down
192.168.0.6 is down
192.168.0.41 is down
192.168.0.49 is down
192.168.0.42 is down
192.168.0.51 is down
192.168.0.33 is down
192.168.0.39 is down
192.168.0.47 is down
192.168.0.87 is down
192.168.0.46 is down
192.168.0.37 is down
192.168.0.53 is down
192.168.0.30 is down
192.168.0.63 is down
192.168.0.58 is down
192.168.0.27 is down
192.168.0.89 is down
192.168.0.36 is down
192.168.0.28 is down
192.168.0.68 is down
192.168.0.4 is down
192.168.0.62 is down
192.168.0.31 is down
192.168.0.29 is down
192.168.0.76 is down
192.168.0.90 is down
192.168.0.22 is down
192.168.0.77 is down
192.168.0.26 is down
192.168.0.96 is down
192.168.0.93 is down
192.168.0.19 is down
192.168.0.102 is down
192.168.0.98 is down
192.168.0.18 is down
192.168.0.101 is down
192.168.0.25 is down
192.168.0.108 is down
192.168.0.24 is down
192.168.0.111 is down
192.168.0.82 is down
192.168.0.16 is down
192.168.0.114 is down
192.168.0.84 is down
192.168.0.21 is down
192.168.0.115 is down
192.168.0.15 is down
192.168.0.14 is down
192.168.0.20 is down
192.168.0.118 is down
192.168.0.92 is down
192.168.0.7 is down
192.168.0.23 is down
192.168.0.100 is down
192.168.0.109 is down
192.168.0.13 is down
192.168.0.135 is down
192.168.0.34 is down
192.168.0.104 is down
192.168.0.12 is down
192.168.0.48 is down
192.168.0.120 is down
192.168.0.45 is down
192.168.0.105 is down
192.168.0.55 is down
192.168.0.122 is down
192.168.0.86 is down
192.168.0.32 is down
192.168.0.60 is down
192.168.0.123 is down
192.168.0.8 is down
192.168.0.107 is down
192.168.0.88 is down
192.168.0.126 is down
192.168.0.10 is down
192.168.0.132 is down
192.168.0.5 is down
192.168.0.106 is down
192.168.0.3 is down
192.168.0.130 is down
192.168.0.112 is down
192.168.0.1 is down
192.168.0.128 is down
192.168.0.124 is down
192.168.0.2 is down
192.168.0.136 is down
192.168.0.127 is down
192.168.0.133 is down
192.168.0.50 is down
192.168.0.134 is down
192.168.0.116 is down
192.168.0.91 is down
192.168.0.11 is down
192.168.0.74 is down
192.168.0.73 is down
192.168.0.129 is down
192.168.0.80 is down
192.168.0.64 is down
192.168.0.57 is down
192.168.0.113 is down
192.168.0.54 is down
192.168.0.97 is down
192.168.0.131 is down
192.168.0.121 is down
192.168.0.119 is down
192.168.0.147 is down
192.168.0.65 is down
192.168.0.70 is down
192.168.0.9 is down
192.168.0.157 is down
192.168.0.125 is down
192.168.0.152 is down
192.168.0.85 is down
192.168.0.140 is down
192.168.0.153 is down
192.168.0.66 is down
192.168.0.143 is down
192.168.0.138 is down
192.168.0.67 is down
192.168.0.145 is down
192.168.0.155 is down
192.168.0.81 is down
192.168.0.148 is down
192.168.0.149 is down
192.168.0.72 is down
192.168.0.160 is down
192.168.0.158 is down
192.168.0.164 is down
192.168.0.83 is down
192.168.0.165 is down
192.168.0.78 is down
192.168.0.159 is down
192.168.0.172 is down
192.168.0.161 is down
192.168.0.79 is down
192.168.0.175 is down
192.168.0.162 is down
192.168.0.150 is down
192.168.0.176 is down
192.168.0.94 is down
192.168.0.166 is down
192.168.0.187 is down
192.168.0.170 is down
192.168.0.167 is down
192.168.0.169 is down
192.168.0.154 is down
192.168.0.181 is down
192.168.0.180 is down
192.168.0.182 is down
192.168.0.185 is down
192.168.0.177 is down
192.168.0.178 is down
192.168.0.95 is down
192.168.0.171 is down
192.168.0.193 is down
192.168.0.184 is down
192.168.0.194 is down
192.168.0.196 is down
192.168.0.188 is down
192.168.0.195 is down
192.168.0.189 is down
192.168.0.191 is down
192.168.0.99 is down
192.168.0.103 is down
192.168.0.199 is down
192.168.0.190 is down
192.168.0.200 is down
192.168.0.198 is down
192.168.0.137 is down
192.168.0.173 is down
192.168.0.139 is down
192.168.0.183 is down
192.168.0.142 is down
192.168.0.110 is down
192.168.0.144 is down
192.168.0.151 is down
192.168.0.197 is down
192.168.0.117 is down
192.168.0.141 is down
192.168.0.163 is down
192.168.0.242 is down
192.168.0.168 is down
192.168.0.220 is down
192.168.0.209 is down
192.168.0.253 is down
192.168.0.205 is down
192.168.0.217 is down
192.168.0.224 is down
192.168.0.218 is down
192.168.0.211 is down
192.168.0.227 is down
192.168.0.226 is down
192.168.0.249 is down
192.168.0.216 is down
192.168.0.239 is down
192.168.0.204 is down
192.168.0.238 is down
192.168.0.222 is down
192.168.0.252 is down
192.168.0.174 is down
192.168.0.221 is down
192.168.0.206 is down
192.168.0.223 is down
192.168.0.203 is down
192.168.0.201 is down
192.168.0.212 is down
192.168.0.244 is down
192.168.0.214 is down
192.168.0.207 is down
192.168.0.232 is down
192.168.0.236 is down
192.168.0.210 is down
192.168.0.146 is down
192.168.0.219 is down
192.168.0.231 is down
192.168.0.225 is down
192.168.0.250 is down
192.168.0.179 is down
192.168.0.229 is down
192.168.0.243 is down
192.168.0.233 is down
192.168.0.230 is down
192.168.0.186 is down
192.168.0.234 is down
192.168.0.208 is down
192.168.0.213 is down
192.168.0.235 is down
192.168.0.254 is down
192.168.0.245 is down
192.168.0.228 is down
192.168.0.192 is down
192.168.0.237 is down
192.168.0.247 is down
192.168.0.156 is down
192.168.0.248 is down
192.168.0.251 is down
192.168.0.240 is down
192.168.0.246 is down
192.168.0.52 is down
192.168.0.69 is down
192.168.0.17 is down
192.168.0.59 is down
192.168.0.202 is down
192.168.0.215 is down
192.168.0.241 is down
[root@localhost /data/scripts]#cat !*
cat for_scan_host.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-27
#FileName: for_scan_host.sh
#********************************************************************
NET=192.168.0
for ID in {1..254};do
	{
	ping -c1 -w1 $NET.$ID &> /dev/null && echo $NET.$ID is up || echo $NET.$ID is down
	}&
done
wait

7.打印九九乘法表

[root@localhost /data/scripts]#bash for_multi_table.sh
1x1=1	
1x2=2	2x2=4	
1x3=3	2x3=6	3x3=9	
1x4=4	2x4=8	3x4=12	4x4=16	
1x5=5	2x5=10	3x5=15	4x5=20	5x5=25	
1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	
1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	
1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	
1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81	
[root@localhost /data/scripts]#cat !*
cat for_multi_table.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-27
#FileName: for_multi_table.sh
#********************************************************************
for i in {1..9};do
	for j in `seq $i`;do
		echo -e "${j}x${i}=$[i*j]\t\c"
	done
	echo
done

8.在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html

[root@localhost /data/scripts]#bash !*
bash for_testdir.sh
Ten files created successfully
[root@localhost /data/scripts]#ll /data/testdir/
total 0
-rw-r--r-- 1 root root 0 Sep 27 15:12 10cVmoFvMz.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 1vJJrUbyU.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 2PIOlJCGz.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 3WxzGmITW.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 4MELzTTxR.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 5hmBzGOkM.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 6KURqHZlg.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 7vdlJCfDJ.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 8HuCDhCWH.html
-rw-r--r-- 1 root root 0 Sep 27 15:12 9YOarxKQR.html
[root@localhost /data/scripts]#cat for_testdir.sh 
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-27
#FileName: for_testdir.sh
#********************************************************************
for((i=1;i<=10;i++));do
	RAN=`tr -dc 'A-Za-z' </dev/urandom | head -c 8`
	touch /data/testdir/${i}${RAN}.html
done
echo "Ten files created successfully"

9.打印等腰三角形

[root@localhost /data/scripts]#bash !*
bash for_triangle.sh
请输入三角形的高度: 10
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
[root@localhost /data/scripts]#cat !*
cat for_triangle.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-27
#FileName: for_triangle.sh
#********************************************************************
read -p "请输入三角形的高度: " line
for((i=1;i<=line;i++));do
	for((k=0;k<=line-i;k++));do
		echo -e ' \c'
	done
	for((j=1;j<=2*i-1;j++));do
		echo -e '*\c'
	done
	echo
done

10.猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?

[root@localhost /data/scripts]#bash !*
bash for_monkey.sh
所摘桃子数:1534
[root@localhost /data/scripts]#cat !*
cat for_monkey.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-09-27
#FileName: for_monkey.sh
#********************************************************************
sum=1
for((n=1;n<10;n++));do
	sum=$[2*$[$sum+1]]
done
echo "所摘桃子数:$sum"
posted @ 2020-09-27 15:34  人生值得  阅读(357)  评论(0编辑  收藏  举报