SHELL脚本编程循环篇-for循环
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.for循环的语法格式
[root@node101.yinzhengjie.org.cn ~]# help for for: for NAME [in WORDS ... ] ; do COMMANDS; done Execute commands for each member in a list. The `for' loop executes a sequence of commands for each member in a list of items. If `in WORDS ...;' is not present, then `in "$@"' is assumed. For each element in WORDS, NAME is set to that element, and the COMMANDS are executed. Exit Status: Returns the status of the last command executed. for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done Arithmetic for loop. Equivalent to (( EXP1 )) while (( EXP2 )); do COMMANDS (( EXP3 )) done EXP1, EXP2, and EXP3 are arithmetic expressions. If any expression is omitted, it behaves as if it evaluates to 1. Exit Status: Returns the status of the last command executed. [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
1>.语法格式一
for 变量 in 值1 值2 值3 ... do 源代码 done
也可以写成一行,案例如下: [root@node101.yinzhengjie.org.cn ~]# for i in {1..100};do let sum1+=i;done;echo sum=$sum1; sum=5050 [root@node101.yinzhengjie.org.cn ~]#
2>.语法格式二(类似C语言风格的变量操作)
for (( 初始值;循环控制条件;变量变化 )) do 源代码 done
也可以写成一行,案例如下: [root@node101.yinzhengjie.org.cn ~]# for((sum=0,i=1;i<=100;i++));do let sum2+=i;done;echo sum=$sum2 sum=5050 [root@node101.yinzhengjie.org.cn ~]#
3>.for循环执行机制
依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束
二.列表生成方式
1>.直接给出列表
[root@node101.yinzhengjie.org.cn ~]# cat shell/list1.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/list.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** declare -i count=1 for i in 1 a 3 b 5 c 7 do echo "arg $count is $i" count=count+1 done [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list1.sh arg 1 is 1 arg 2 is a arg 3 is 3 arg 4 is b arg 5 is 5 arg 6 is c arg 7 is 7 [root@node101.yinzhengjie.org.cn ~]#
2>.整数列表
[root@node101.yinzhengjie.org.cn ~]# cat shell/list2.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/list2.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** declare -i count=1 for i in {A..z} do echo "arg $count is $i" count=count+1 done [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list2.sh arg 1 is A arg 2 is B arg 3 is C arg 4 is D arg 5 is E arg 6 is F arg 7 is G arg 8 is H arg 9 is I arg 10 is J arg 11 is K arg 12 is L arg 13 is M arg 14 is N arg 15 is O arg 16 is P arg 17 is Q arg 18 is R arg 19 is S arg 20 is T arg 21 is U arg 22 is V arg 23 is W arg 24 is X arg 25 is Y arg 26 is Z arg 27 is [ arg 28 is arg 29 is ] arg 30 is ^ arg 31 is _ arg 32 is ` arg 33 is a arg 34 is b arg 35 is c arg 36 is d arg 37 is e arg 38 is f arg 39 is g arg 40 is h arg 41 is i arg 42 is j arg 43 is k arg 44 is l arg 45 is m arg 46 is n arg 47 is o arg 48 is p arg 49 is q arg 50 is r arg 51 is s arg 52 is t arg 53 is u arg 54 is v arg 55 is w arg 56 is x arg 57 is y arg 58 is z [root@node101.yinzhengjie.org.cn ~]#
3>.返回列表的命令
[root@node101.yinzhengjie.org.cn ~]# cat shell/list3.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/list3.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** declare -i count=1 for i in `seq 100 3 200` do echo "arg $count is $i" count=count+1 done [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list3.sh arg 1 is 100 arg 2 is 103 arg 3 is 106 arg 4 is 109 arg 5 is 112 arg 6 is 115 arg 7 is 118 arg 8 is 121 arg 9 is 124 arg 10 is 127 arg 11 is 130 arg 12 is 133 arg 13 is 136 arg 14 is 139 arg 15 is 142 arg 16 is 145 arg 17 is 148 arg 18 is 151 arg 19 is 154 arg 20 is 157 arg 21 is 160 arg 22 is 163 arg 23 is 166 arg 24 is 169 arg 25 is 172 arg 26 is 175 arg 27 is 178 arg 28 is 181 arg 29 is 184 arg 30 is 187 arg 31 is 190 arg 32 is 193 arg 33 is 196 arg 34 is 199 [root@node101.yinzhengjie.org.cn ~]#
4>.使用glob,如:"*.sh"
[root@node101.yinzhengjie.org.cn ~]# cat shell/list4.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/list4.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** declare -i count=1 for i in /etc/profile.d/*.sh do echo "arg $count is $i" count=count+1 done [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list4.sh arg 1 is /etc/profile.d/256term.sh arg 2 is /etc/profile.d/bash_completion.sh arg 3 is /etc/profile.d/colorgrep.sh arg 4 is /etc/profile.d/colorls.sh arg 5 is /etc/profile.d/crond.sh arg 6 is /etc/profile.d/lang.sh arg 7 is /etc/profile.d/less.sh arg 8 is /etc/profile.d/qt-graphicssystem.sh arg 9 is /etc/profile.d/qt.sh arg 10 is /etc/profile.d/vim.sh arg 11 is /etc/profile.d/which2.sh [root@node101.yinzhengjie.org.cn ~]#
5>.位置变量引用
[root@node101.yinzhengjie.org.cn ~]# cat shell/list5.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/list4.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** declare -i count=1 for i in $@ do echo "arg $count is $i" count=count+1 done [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list5.sh python java golang shell c++ php arg 1 is python arg 2 is java arg 3 is golang arg 4 is shell arg 5 is c++ arg 6 is php [root@node101.yinzhengjie.org.cn ~]#
三.请用for循环实现以下小练习
1>.计算1到100之间的和
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh [root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/sum.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** sum=0 for i in {1..100};do let sum=sum+i done echo "1到100的和为: $sum" [root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh 1到100的和为: 5050 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/sum.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** sum=0 for i in {1..100};do let sum=$[sum+i] done echo "1到100的和为: $sum" [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh 1到100的和为: 5050 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh [root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/sum.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** sum=0 for i in {1..100};do let sum=$((sum+i)) done echo "1到100的和为: $sum" [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh 1到100的和为: 5050 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/sum.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** declare -i sum=0 for i in {1..100};do sum=sum+i done echo "1到100的和为: $sum" [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh 1到100的和为: 5050 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/sum.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** sum=0 read -t 30 -p "Please enter the start number>>> " StartNumber read -t 30 -p "Please enter an end number>>> " EndNumber for ((i=$StartNumber;i<=$EndNumber;i=i+1)) do sum=$(($sum+$i)) done echo "从$StartNumber加到$EndNumber的总和是:$sum" [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh Please enter the start number>>> 1 Please enter an end number>>> 100 从1加到100的总和是:5050 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
2>.猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?
[root@node101.yinzhengjie.org.cn ~]# cat shell/monkey.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/monkey.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** sum=1 for i in {9..1} do let sum=(sum+1)*2 done echo "桃子的个数是: $sum" unset sum [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh 桃子的个数是: 1534 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/monkey.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/monkey.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** read -p "请输入天数: " day read -p "请输入最后一天剩余个数: " sum let day=day-1 for i in `seq 1 $day` do let sum=(sum+1)*2 done echo "桃子的个数是: $sum" unset sum [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh 请输入天数: 10 请输入最后一天剩余个数: 1 桃子的个数是: 1534 [root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh 请输入天数: 32 请输入最后一天剩余个数: 2 桃子的个数是: 8589934590 [root@node101.yinzhengjie.org.cn ~]#
3>.将一个目录的所有文件后缀都更改为"*.log"
[root@node101.yinzhengjie.org.cn ~]# ll ~/backup/ total 0 -rw-r--r-- 1 root root 0 Nov 25 09:47 1.pdf -rw-r--r-- 1 root root 0 Nov 25 09:47 aa.png -rw-r--r-- 1 root root 0 Nov 25 09:50 a.b.c.txt -rw-r--r-- 1 root root 0 Nov 25 09:47 a.txt -rw-r--r-- 1 root root 0 Nov 25 09:47 b1.rmvb -rw-r--r-- 1 root root 0 Nov 25 09:47 b.jpg [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat shell/rename.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/rename.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** for i in /root/backup/* do basename=`echo $i | sed -nr 's#(.*)..*#1#p'` mv $i ${basename}.log done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/rename.sh [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll ~/backup/ total 0 -rw-r--r-- 1 root root 0 Nov 25 09:47 1.log -rw-r--r-- 1 root root 0 Nov 25 09:47 aa.log -rw-r--r-- 1 root root 0 Nov 25 09:50 a.b.c.log -rw-r--r-- 1 root root 0 Nov 25 09:47 a.log -rw-r--r-- 1 root root 0 Nov 25 09:47 b1.log -rw-r--r-- 1 root root 0 Nov 25 09:47 b.log [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/rename.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/rename.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** file_path=/root/backup/ cd $file_path for suffix in `ls | sed -nr 's/.*.(.*)/1/p' | sort -u` do rename "$suffix" "log" * done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll ~/backup total 0 -rw-r--r-- 1 root root 0 Nov 25 09:47 1.pdf -rw-r--r-- 1 root root 0 Nov 25 09:47 aa.png -rw-r--r-- 1 root root 0 Nov 25 09:50 a.b.c.txt -rw-r--r-- 1 root root 0 Nov 25 09:47 a.txt -rw-r--r-- 1 root root 0 Nov 25 09:47 b1.rmvb -rw-r--r-- 1 root root 0 Nov 25 09:47 b.jpg [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/rename.sh [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll ~/backup total 0 -rw-r--r-- 1 root root 0 Nov 25 09:47 1.log -rw-r--r-- 1 root root 0 Nov 25 09:47 aa.log -rw-r--r-- 1 root root 0 Nov 25 09:50 a.b.c.log -rw-r--r-- 1 root root 0 Nov 25 09:47 a.log -rw-r--r-- 1 root root 0 Nov 25 09:47 b1.log -rw-r--r-- 1 root root 0 Nov 25 09:47 b.log [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
4>.编写脚本,提示请输入网络地址,如192.168.1.0,判断输入的网段中主机在线状态
[root@node101.yinzhengjie.org.cn ~]# cat shell/scanip.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/scanip.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** netid=172.30.1 for id in {1..254} do #并行执行下面的ping操作 { if ping -c 1 -w 1 $netid.$id &>/dev/null ;then echo "$netid.$id is up!" else echo "$netid.$id is down!" fi }& done #等待后台执行的命令执行完毕后自动退出 wait [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/scanip.sh 172.30.1.101 is up! 172.30.1.102 is up! 172.30.1.253 is up! 172.30.1.1 is down! 172.30.1.3 is down! 172.30.1.2 is down! 172.30.1.9 is down! 172.30.1.6 is down! 172.30.1.5 is down! 172.30.1.12 is down! 172.30.1.15 is down! 172.30.1.10 is down! 172.30.1.7 is down! 172.30.1.19 is down! 172.30.1.14 is down! 172.30.1.18 is down! 172.30.1.23 is down! 172.30.1.13 is down! 172.30.1.25 is down! 172.30.1.20 is down! 172.30.1.29 is down! 172.30.1.27 is down! 172.30.1.24 is down! 172.30.1.22 is down! 172.30.1.31 is down! 172.30.1.33 is down! 172.30.1.35 is down! 172.30.1.36 is down! 172.30.1.28 is down! 172.30.1.37 is down! 172.30.1.38 is down! 172.30.1.8 is down! 172.30.1.66 is down! 172.30.1.58 is down! 172.30.1.43 is down! 172.30.1.62 is down! 172.30.1.48 is down! 172.30.1.46 is down! 172.30.1.55 is down! 172.30.1.32 is down! 172.30.1.51 is down! 172.30.1.41 is down! 172.30.1.53 is down! 172.30.1.42 is down! 172.30.1.63 is down! 172.30.1.47 is down! 172.30.1.50 is down! 172.30.1.59 is down! 172.30.1.11 is down! 172.30.1.71 is down! 172.30.1.54 is down! 172.30.1.52 is down! 172.30.1.72 is down! 172.30.1.70 is down! 172.30.1.81 is down! 172.30.1.61 is down! 172.30.1.44 is down! 172.30.1.74 is down! 172.30.1.65 is down! 172.30.1.69 is down! 172.30.1.78 is down! 172.30.1.88 is down! 172.30.1.57 is down! 172.30.1.17 is down! 172.30.1.83 is down! 172.30.1.77 is down! 172.30.1.68 is down! 172.30.1.73 is down! 172.30.1.84 is down! 172.30.1.82 is down! 172.30.1.89 is down! 172.30.1.86 is down! 172.30.1.16 is down! 172.30.1.21 is down! 172.30.1.93 is down! 172.30.1.26 is down! 172.30.1.92 is down! 172.30.1.30 is down! 172.30.1.97 is down! 172.30.1.96 is down! 172.30.1.40 is down! 172.30.1.108 is down! 172.30.1.87 is down! 172.30.1.103 is down! 172.30.1.109 is down! 172.30.1.104 is down! 172.30.1.34 is down! 172.30.1.91 is down! 172.30.1.110 is down! 172.30.1.45 is down! 172.30.1.100 is down! 172.30.1.95 is down! 172.30.1.129 is down! 172.30.1.4 is down! 172.30.1.49 is down! 172.30.1.67 is down! 172.30.1.106 is down! 172.30.1.107 is down! 172.30.1.56 is down! 172.30.1.116 is down! 172.30.1.113 is down! 172.30.1.39 is down! 172.30.1.60 is down! 172.30.1.121 is down! 172.30.1.111 is down! 172.30.1.64 is down! 172.30.1.125 is down! 172.30.1.75 is down! 172.30.1.119 is down! 172.30.1.130 is down! 172.30.1.123 is down! 172.30.1.138 is down! 172.30.1.115 is down! 172.30.1.79 is down! 172.30.1.147 is down! 172.30.1.118 is down! 172.30.1.80 is down! 172.30.1.127 is down! 172.30.1.148 is down! 172.30.1.114 is down! 172.30.1.94 is down! 172.30.1.134 is down! 172.30.1.85 is down! 172.30.1.124 is down! 172.30.1.90 is down! 172.30.1.128 is down! 172.30.1.142 is down! 172.30.1.137 is down! 172.30.1.98 is down! 172.30.1.132 is down! 172.30.1.144 is down! 172.30.1.152 is down! 172.30.1.131 is down! 172.30.1.140 is down! 172.30.1.157 is down! 172.30.1.136 is down! 172.30.1.76 is down! 172.30.1.219 is down! 172.30.1.99 is down! 172.30.1.133 is down! 172.30.1.155 is down! 172.30.1.189 is down! 172.30.1.151 is down! 172.30.1.162 is down! 172.30.1.175 is down! 172.30.1.105 is down! 172.30.1.176 is down! 172.30.1.180 is down! 172.30.1.166 is down! 172.30.1.158 is down! 172.30.1.170 is down! 172.30.1.179 is down! 172.30.1.161 is down! 172.30.1.215 is down! 172.30.1.167 is down! 172.30.1.184 is down! 172.30.1.211 is down! 172.30.1.185 is down! 172.30.1.218 is down! 172.30.1.143 is down! 172.30.1.173 is down! 172.30.1.165 is down! 172.30.1.146 is down! 172.30.1.188 is down! 172.30.1.181 is down! 172.30.1.193 is down! 172.30.1.120 is down! 172.30.1.172 is down! 172.30.1.169 is down! 172.30.1.122 is down! 172.30.1.214 is down! 172.30.1.187 is down! 172.30.1.126 is down! 172.30.1.174 is down! 172.30.1.159 is down! 172.30.1.192 is down! 172.30.1.203 is down! 172.30.1.168 is down! 172.30.1.160 is down! 172.30.1.177 is down! 172.30.1.205 is down! 172.30.1.139 is down! 172.30.1.150 is down! 172.30.1.202 is down! 172.30.1.163 is down! 172.30.1.199 is down! 172.30.1.145 is down! 172.30.1.198 is down! 172.30.1.222 is down! 172.30.1.206 is down! 172.30.1.207 is down! 172.30.1.135 is down! 172.30.1.171 is down! 172.30.1.141 is down! 172.30.1.210 is down! 172.30.1.213 is down! 172.30.1.164 is down! 172.30.1.225 is down! 172.30.1.149 is down! 172.30.1.156 is down! 172.30.1.183 is down! 172.30.1.178 is down! 172.30.1.200 is down! 172.30.1.228 is down! 172.30.1.216 is down! 172.30.1.217 is down! 172.30.1.196 is down! 172.30.1.195 is down! 172.30.1.209 is down! 172.30.1.182 is down! 172.30.1.186 is down! 172.30.1.191 is down! 172.30.1.234 is down! 172.30.1.232 is down! 172.30.1.190 is down! 172.30.1.244 is down! 172.30.1.230 is down! 172.30.1.237 is down! 172.30.1.212 is down! 172.30.1.235 is down! 172.30.1.223 is down! 172.30.1.220 is down! 172.30.1.254 is down! 172.30.1.240 is down! 172.30.1.238 is down! 172.30.1.252 is down! 172.30.1.197 is down! 172.30.1.226 is down! 172.30.1.247 is down! 172.30.1.246 is down! 172.30.1.208 is down! 172.30.1.112 is down! 172.30.1.231 is down! 172.30.1.233 is down! 172.30.1.204 is down! 172.30.1.236 is down! 172.30.1.229 is down! 172.30.1.242 is down! 172.30.1.245 is down! 172.30.1.153 is down! 172.30.1.248 is down! 172.30.1.224 is down! 172.30.1.154 is down! 172.30.1.251 is down! 172.30.1.221 is down! 172.30.1.249 is down! 172.30.1.241 is down! 172.30.1.194 is down! 172.30.1.239 is down! 172.30.1.243 is down! 172.30.1.201 is down! 172.30.1.117 is down! 172.30.1.227 is down! 172.30.1.250 is down! [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/scanip.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/scanip.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** read -p "Please input a netid(eg:192.168.1.0) " netid host_list="/tmp/iplist.log" #每次扫描文件时先清空上次的记录 >$host_list netid=`echo $netid | sed -nr 's#(.*)..*#1#p'` echo netid=$netid for id in {1..254} do #并行执行下面的ping操作 { if ping -c 1 -w 1 $netid.$id &>/dev/null ;then #将能ping通的主机地址保存下来 echo "$netid.$id" >> $host_list echo "$netid.$id is up!" else echo "$netid.$id is down!" fi }& done #等待后台执行的命令执行完毕后自动退出 wait [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/scanip.sh Please input a netid(eg:192.168.1.0) 172.30.1.0 netid=172.30.1 172.30.1.101 is up! 172.30.1.102 is up! 172.30.1.253 is up! 172.30.1.1 is down! 172.30.1.5 is down! 172.30.1.2 is down! 172.30.1.6 is down! 172.30.1.3 is down! 172.30.1.16 is down! 172.30.1.8 is down! 172.30.1.4 is down! 172.30.1.20 is down! 172.30.1.12 is down! 172.30.1.10 is down! 172.30.1.9 is down! 172.30.1.19 is down! 172.30.1.14 is down! 172.30.1.21 is down! 172.30.1.24 is down! 172.30.1.18 is down! 172.30.1.13 is down! 172.30.1.25 is down! 172.30.1.17 is down! 172.30.1.27 is down! 172.30.1.28 is down! 172.30.1.30 is down! 172.30.1.31 is down! 172.30.1.22 is down! 172.30.1.33 is down! 172.30.1.32 is down! 172.30.1.35 is down! 172.30.1.26 is down! 172.30.1.76 is down! 172.30.1.41 is down! 172.30.1.38 is down! 172.30.1.48 is down! 172.30.1.45 is down! 172.30.1.42 is down! 172.30.1.36 is down! 172.30.1.49 is down! 172.30.1.47 is down! 172.30.1.40 is down! 172.30.1.53 is down! 172.30.1.51 is down! 172.30.1.43 is down! 172.30.1.54 is down! 172.30.1.58 is down! 172.30.1.46 is down! 172.30.1.56 is down! 172.30.1.59 is down! 172.30.1.62 is down! 172.30.1.7 is down! 172.30.1.73 is down! 172.30.1.29 is down! 172.30.1.61 is down! 172.30.1.50 is down! 172.30.1.34 is down! 172.30.1.68 is down! 172.30.1.65 is down! 172.30.1.63 is down! 172.30.1.67 is down! 172.30.1.11 is down! 172.30.1.69 is down! 172.30.1.15 is down! 172.30.1.55 is down! 172.30.1.71 is down! 172.30.1.37 is down! 172.30.1.66 is down! 172.30.1.60 is down! 172.30.1.77 is down! 172.30.1.39 is down! 172.30.1.79 is down! 172.30.1.75 is down! 172.30.1.44 is down! 172.30.1.64 is down! 172.30.1.57 is down! 172.30.1.70 is down! 172.30.1.74 is down! 172.30.1.78 is down! 172.30.1.82 is down! 172.30.1.52 is down! 172.30.1.83 is down! 172.30.1.72 is down! 172.30.1.23 is down! 172.30.1.87 is down! 172.30.1.84 is down! 172.30.1.85 is down! 172.30.1.89 is down! 172.30.1.88 is down! 172.30.1.93 is down! 172.30.1.90 is down! 172.30.1.98 is down! 172.30.1.94 is down! 172.30.1.96 is down! 172.30.1.99 is down! 172.30.1.100 is down! 172.30.1.95 is down! 172.30.1.103 is down! 172.30.1.104 is down! 172.30.1.112 is down! 172.30.1.110 is down! 172.30.1.108 is down! 172.30.1.109 is down! 172.30.1.113 is down! 172.30.1.122 is down! 172.30.1.105 is down! 172.30.1.106 is down! 172.30.1.126 is down! 172.30.1.115 is down! 172.30.1.117 is down! 172.30.1.118 is down! 172.30.1.120 is down! 172.30.1.116 is down! 172.30.1.129 is down! 172.30.1.124 is down! 172.30.1.119 is down! 172.30.1.131 is down! 172.30.1.128 is down! 172.30.1.142 is down! 172.30.1.143 is down! 172.30.1.138 is down! 172.30.1.123 is down! 172.30.1.133 is down! 172.30.1.145 is down! 172.30.1.147 is down! 172.30.1.127 is down! 172.30.1.80 is down! 172.30.1.155 is down! 172.30.1.151 is down! 172.30.1.81 is down! 172.30.1.132 is down! 172.30.1.140 is down! 172.30.1.144 is down! 172.30.1.86 is down! 172.30.1.160 is down! 172.30.1.134 is down! 172.30.1.157 is down! 172.30.1.164 is down! 172.30.1.148 is down! 172.30.1.139 is down! 172.30.1.149 is down! 172.30.1.159 is down! 172.30.1.153 is down! 172.30.1.168 is down! 172.30.1.161 is down! 172.30.1.154 is down! 172.30.1.169 is down! 172.30.1.171 is down! 172.30.1.174 is down! 172.30.1.135 is down! 172.30.1.166 is down! 172.30.1.179 is down! 172.30.1.172 is down! 172.30.1.158 is down! 172.30.1.182 is down! 172.30.1.111 is down! 172.30.1.180 is down! 172.30.1.163 is down! 172.30.1.152 is down! 172.30.1.121 is down! 172.30.1.185 is down! 172.30.1.125 is down! 172.30.1.177 is down! 172.30.1.170 is down! 172.30.1.190 is down! 172.30.1.183 is down! 172.30.1.130 is down! 172.30.1.165 is down! 172.30.1.194 is down! 172.30.1.136 is down! 172.30.1.192 is down! 172.30.1.175 is down! 172.30.1.188 is down! 172.30.1.137 is down! 172.30.1.146 is down! 172.30.1.195 is down! 172.30.1.176 is down! 172.30.1.150 is down! 172.30.1.181 is down! 172.30.1.156 is down! 172.30.1.186 is down! 172.30.1.187 is down! 172.30.1.162 is down! 172.30.1.107 is down! 172.30.1.191 is down! 172.30.1.184 is down! 172.30.1.141 is down! 172.30.1.92 is down! 172.30.1.193 is down! 172.30.1.197 is down! 172.30.1.114 is down! 172.30.1.189 is down! 172.30.1.167 is down! 172.30.1.178 is down! 172.30.1.223 is down! 172.30.1.91 is down! 172.30.1.203 is down! 172.30.1.230 is down! 172.30.1.196 is down! 172.30.1.204 is down! 172.30.1.241 is down! 172.30.1.198 is down! 172.30.1.205 is down! 172.30.1.208 is down! 172.30.1.249 is down! 172.30.1.97 is down! 172.30.1.220 is down! 172.30.1.210 is down! 172.30.1.200 is down! 172.30.1.201 is down! 172.30.1.209 is down! 172.30.1.207 is down! 172.30.1.219 is down! 172.30.1.217 is down! 172.30.1.202 is down! 172.30.1.212 is down! 172.30.1.224 is down! 172.30.1.252 is down! 172.30.1.218 is down! 172.30.1.206 is down! 172.30.1.246 is down! 172.30.1.245 is down! 172.30.1.232 is down! 172.30.1.222 is down! 172.30.1.231 is down! 172.30.1.213 is down! 172.30.1.227 is down! 172.30.1.211 is down! 172.30.1.226 is down! 172.30.1.215 is down! 172.30.1.236 is down! 172.30.1.173 is down! 172.30.1.234 is down! 172.30.1.225 is down! 172.30.1.243 is down! 172.30.1.248 is down! 172.30.1.239 is down! 172.30.1.254 is down! 172.30.1.233 is down! 172.30.1.235 is down! 172.30.1.221 is down! 172.30.1.244 is down! 172.30.1.251 is down! 172.30.1.229 is down! 172.30.1.247 is down! 172.30.1.250 is down! 172.30.1.237 is down! 172.30.1.216 is down! 172.30.1.240 is down! 172.30.1.228 is down! 172.30.1.242 is down! 172.30.1.238 is down! 172.30.1.199 is down! 172.30.1.214 is down! [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /tmp/iplist.log 172.30.1.101 172.30.1.102 172.30.1.253 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
5>.让用户输入行数和列数,编写脚本自动打印该矩形
[root@node101.yinzhengjie.org.cn ~]# cat shell/rectangle.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/rectangle.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** read -p "Please input colume: " col read -p "Please input line: " line for i in `seq $line`;do for j in `seq $col`;do echo -n "*" done #内循环结束后添加一个换行符 echo done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/rectangle.sh Please input colume: 10 Please input line: 5 ********** ********** ********** ********** ********** [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/rectangle.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/rectangle.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** read -p "Please input colume: " col read -p "Please input line: " line for i in `seq $line`;do for j in `seq $col`;do #定义随机颜色 COLOR=$[RANDOM%7+31] #输出时使用上面获取的随机颜色并高亮 #echo -e "