天天看點

Shell腳本 - 程式設計進階01( for循環 )

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