欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Shell脚本 - 编程进阶01( for循环 )

程序员文章站 2022-07-10 11:18:32
...
1、判断 /var/ 目录下所有文件的类型;
  • 脚本
#! /bin/bash
path='/var'
for files in `ls $path`;do
	filetype='None'
	file=$path/$files
	[ -b $file ] && filetype=Block
	[ -c $file ] && filetype=Char
	[ -d $file ] && filetype=Dirtory
	[ -f $file ] && filetype=Normal
	[ -h $file ] && filetype=Link
	[ -p $file ] && filetype=Pipe
	[ -S $file ] && filetype=Socket
	echo $file is a $filetype File
done
  • 执行结果
[[email protected] ~]# ./for_file_type.sh 
/var/account is a Dirtory File
/var/adm is a Dirtory File
/var/cache is a Dirtory File
/var/crash is a Dirtory File
/var/db is a Dirtory File
/var/empty is a Dirtory File
/var/games is a Dirtory File
/var/gopher is a Dirtory File
/var/kerberos is a Dirtory File
/var/laaa12bbb is a Normal File
/var/lib is a Dirtory File
/var/local is a Dirtory File
/var/lock is a Link File
/var/log is a Dirtory File
/var/mail is a Link File
/var/nis is a Dirtory File
/var/opt is a Dirtory File
/var/preserve is a Dirtory File
/var/run is a Link File
/var/spool is a Dirtory File
/var/target is a Dirtory File
/var/tmp is a Dirtory File
/var/yp is a Dirtory File
2、添加 10 个用户 user1-user10,密码为 8 位随机字符;
  • 脚本
#!/bin/bash
random=''
for i in {1..10};do
	password=`cat /dev/urandom | tr -dc [[:alpha:]] | head -c8`
	if ! [ id user$i &> /dev/null ]
	then
		useradd user$i
		echo $password | passwd user$i --stdin &>/dev/null
		echo user:user$i,password:$password
	else
		echo "User user$i alreadey exist."
	fi	
done
  • 执行结果
[[email protected] ~]# ./for_useradd.sh 
user:user1,password:KEexFhv]
user:user2,password:sSkoIaSp
user:user3,password:opZEMuVi
user:user4,password:[aVCgPPw
user:user5,password:mwdZdEsJ
user:user6,password:QsLSDMkG
user:user7,password:ybPndZQw
user:user8,password:ZpNRbbSg
user:user9,password:KzXCyNQT
user:user10,password:OWQfMFTN
[[email protected] ~]# getent passwd | grep '^user'
user1:x:1015:1015::/home/user1:/bin/bash
user2:x:1016:1016::/home/user2:/bin/bash
user3:x:1017:1017::/home/user3:/bin/bash
user4:x:1018:1018::/home/user4:/bin/bash
user5:x:1019:1019::/home/user5:/bin/bash
user6:x:1020:1020::/home/user6:/bin/bash
user7:x:1021:1021::/home/user7:/bin/bash
user8:x:1022:1022::/home/user8:/bin/bash
user9:x:1023:1023::/home/user9:/bin/bash
user10:x:1024:1024::/home/user10:/bin/bash
3、/etc/rc.d/rc3.d 目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加 stop,以 S 开头的输出为文件名加 start,如 K34filename stop S66filename start;
  • 脚本
#! /bin/bash
for i in `ls /etc/rc.d/rc3.d`;do
	START=`echo $i | head -c1`
	[ $START == K ] && echo $i stop
	[ $START == S ] && echo $i start
done
  • 执行结果
[[email protected] ~]# ./for_rc3.d.sh 
K50netconsole stop
S10network start
4、编写脚本,提示输入正整数 n 的值,计算 1+2+…+n 的总和;
  • 脚本
#! /bin/bash
while [ 1 ]
do
	read -p "Please input a numirc :" N	
	if [[ ${N} =~ ^[0-9]+$ ]];then
		break
	else
		echo Error , Please input again.
		continue
	fi
done
SUM=0
for i in `seq $N`;do
	let SUM+=$i
done
echo the sum is $SUM
  • 执行结果
[[email protected] ~]# ./for_sum_n.sh 
Please input a numirc :10
the sum is 55
[[email protected] ~]# ./for_sum_n.sh 
Please input a numirc :100
the sum is 5050
5、计算 100 以内所有能被 3 整除的整数之和;
  • 脚本
#! /bin/bash
SUM=0
for i in {1..100};do
	if [ $[$i%3] -eq 0 ]
	then
		let SUM+=$i
	fi 
done
echo The sum is $SUM
  • 执行结果
[[email protected] ~]# ./for_100div3.sh 
The sum is 1683
6、编写脚本,提示请输入网络地址,如 192.168.0.0,判断输入的网段中主机在线状态;
  • 脚本
#!/bin/bash
NET=192.168.31.
for i in {1..254};do
	{
		if ping -c 2 -w 3 $NET$i &> /dev/null
		then
			echo $NET$i is up
		else
			echo $NET$i is down
		fi
	} &
done
wait
  • 执行结果
[[email protected] ~]# ./for_ping.sh
10.10.10.1 is up
10.10.10.2 is up
10.10.10.10 is up
10.10.10.66 is up
10.10.10.3 is down
10.10.10.8 is down
10.10.10.16 is down
10.10.10.11 is down
10.10.10.24 is down
10.10.10.14 is down
10.10.10.5 is down
10.10.10.12 is down
10.10.10.20 is down
10.10.10.7 is down
10.10.10.4 is down
10.10.10.64 is down
10.10.10.27 is down
10.10.10.33 is down
10.10.10.18 is down
....以下内容省略....
7、打印九九乘法表;
  • 脚本
#!/bin/bash
for ((i=1;i<=9;i++));do
	for ((j=1;j<=$i;j++));do
		echo -ne "$j * $i = $((j*i))\t"
	done
	echo
done
echo
for ((i=9;i>0;i--));do
	for((j=1;j<=i;j++));do
		echo -ne "$j * $i = $((j*i))\t"
	done
	echo
done
  • 执行结果
[[email protected] ~]# ./for_jiujiubiao.sh 
1 * 1 = 1	
1 * 2 = 2	2 * 2 = 4	
1 * 3 = 3	2 * 3 = 6	3 * 3 = 9	
1 * 4 = 4	2 * 4 = 8	3 * 4 = 12	4 * 4 = 16	
1 * 5 = 5	2 * 5 = 10	3 * 5 = 15	4 * 5 = 20	5 * 5 = 25	
1 * 6 = 6	2 * 6 = 12	3 * 6 = 18	4 * 6 = 24	5 * 6 = 30	6 * 6 = 36	
1 * 7 = 7	2 * 7 = 14	3 * 7 = 21	4 * 7 = 28	5 * 7 = 35	6 * 7 = 42	7 * 7 = 49	
1 * 8 = 8	2 * 8 = 16	3 * 8 = 24	4 * 8 = 32	5 * 8 = 40	6 * 8 = 48	7 * 8 = 56	8 * 8 = 64	
1 * 9 = 9	2 * 9 = 18	3 * 9 = 27	4 * 9 = 36	5 * 9 = 45	6 * 9 = 54	7 * 9 = 63	8 * 9 = 72	9 * 9 = 81	

1 * 9 = 9	2 * 9 = 18	3 * 9 = 27	4 * 9 = 36	5 * 9 = 45	6 * 9 = 54	7 * 9 = 63	8 * 9 = 72	9 * 9 = 81	
1 * 8 = 8	2 * 8 = 16	3 * 8 = 24	4 * 8 = 32	5 * 8 = 40	6 * 8 = 48	7 * 8 = 56	8 * 8 = 64	
1 * 7 = 7	2 * 7 = 14	3 * 7 = 21	4 * 7 = 28	5 * 7 = 35	6 * 7 = 42	7 * 7 = 49	
1 * 6 = 6	2 * 6 = 12	3 * 6 = 18	4 * 6 = 24	5 * 6 = 30	6 * 6 = 36	
1 * 5 = 5	2 * 5 = 10	3 * 5 = 15	4 * 5 = 20	5 * 5 = 25	
1 * 4 = 4	2 * 4 = 8	3 * 4 = 12	4 * 4 = 16	
1 * 3 = 3	2 * 3 = 6	3 * 3 = 9	
1 * 2 = 2	2 * 2 = 4	
1 * 1 = 1	
8、在 /testdir 目录下创建 10 个 html 文件,文件名格式为数字 N(从1到10)加随机 8 个字母,如:1AbCdeFgH.html;
  • 脚本
#!/bin/bash
[ -d /testdir ] || mkdir /testdir
random=''
for i in {1..10};do
	random=`cat /dev/urandom | tr -dc [[:alpha:]] | head -c8`
	touch /testdir/$i$random.html
done
  • 执行结果
[[email protected] ~]# ./for_10html.sh 
[[email protected] ~]# ll /testdir/
total 0
-rw-r--r--. 1 root root 0 Jun  2 21:05 10sknGGqXx.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 1ASVfnVsG.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 2ozFgFMnN.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 3wWlpguSa.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 4gu]IYuxX.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 5RLFfRPRc.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 6nmVqNDhT.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 7ixTqGubd.html
-rw-r--r--. 1 root root 0 Jun  2 21:05 8MypZzqz].html
-rw-r--r--. 1 root root 0 Jun  2 21:05 9RQkdkvax.html
9、打印等腰三角形;
  • 脚本
#!/bin/bash
while [ 1 ]
do
	read -p "Please input a numirc :" N
	if [[ ${N} =~ ^[0-9]+$ ]];then
		break
	else
		echo Error , Please input again.
		continue
	fi
done
for i in `seq $N`;do
	for((n=1;n<=$((N-i));n++));do
		echo -n ' '
	done
	for((j=1;j<=$((i*2-1));j++));do
		echo -n "*"
	done
	echo
done
  • 执行结果
[[email protected] ~]# ./for_triangle.sh 
Please input a numirc :5
    *
   ***
  *****
 *******
*********
[[email protected] ~]# ./for_triangle.sh 
Please input a numirc :10
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
10、猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?
  • 脚本
#!/bin/bash
taozhi=1
echo "第10天:$taozhi"
for((i=10;i>1;i--));do                                                                                       
    let taozhi++
    let taozhi*=2
    echo "第$[i-1]天:$taozhi"
done
  • 执行结果
[[email protected] ~]# ./for_taozhi.sh 
第10天:1
第9天:4
第8天:10
第7天:22
第6天:46
第5天:94
第4天:190
第3天:382
第2天:766
第1天:1534
相关标签: Shell 脚本