SHELL脚本编程循环篇-for循环
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 "\033[1;${COLOR}m*\033[0m\c" #输出时使用上面获取的随机颜色并高亮且附带闪烁功能 echo -e "\033[1;5;${COLOR}m*\033[0m\c" 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 ~]# 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 if [ $i -eq 1 -o $i -eq $line -o $j -eq 1 -o $j -eq $col ];then #定义随机颜色 COLOR=$[RANDOM%7+31] #输出时使用上面获取的随机颜色并高亮且附带闪烁功能 echo -e "\033[1;5;${COLOR}m*\033[0m\c" else echo -n "*" fi done #内循环结束后添加一个换行符 echo done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/rectangle.sh Please input colume: 20 Please input line: 15 ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** ******************** [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 case $i in 1|$line) #定义随机颜色 COLOR=$[RANDOM%7+31] #输出时使用上面获取的随机颜色并高亮且附带闪烁功能 echo -e "\033[1;5;${COLOR}m*\033[0m\c" ;; *) case $j in 1|$col) COLOR=$[RANDOM%7+31] echo -e "\033[1;5;${COLOR}m*\033[0m\c" ;; *) echo -e "*\c" ;; esac esac 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 ~]#
6>.打印等腰三角形
[root@node101.yinzhengjie.org.cn ~]# cat shell/triangle.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/triangle.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 line : " line for i in `seq $line`;do let star=$i*2-1 let space=$line-$i for j in `seq $space`;do echo -n " " done for k in `seq $star`;do echo -e "*\c" done echo done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/triangle.sh Please input line : 10 * *** ***** ******* ********* *********** ************* *************** ***************** ******************* [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/triangle.sh Please input line : 5 * *** ***** ******* ********* [root@node101.yinzhengjie.org.cn ~]#
7>.打印九九乘法表
[root@node101.yinzhengjie.org.cn ~]# cat shell/99.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/99.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 {1..9};do for j in `seq $i`;do let sum=i*j echo -e "${j} x ${i} = $sum\t\c" done echo done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/99.sh 1 x 1 = 1 1 x 2 = 2 2 x 2 = 4 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49 1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/99.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/99.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 "请输入您想要打印的乘法表行数: " line if [ -z $line ];then line=9 fi for i in `seq $line`;do for j in `seq $i`;do echo -e "${j} x ${i} = $((i*j))\t\c" done echo done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/99.sh 请输入您想要打印的乘法表行数: 8 1 x 1 = 1 1 x 2 = 2 2 x 2 = 4 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49 1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/99.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/99.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 {9..1};do for j in `seq $i`;do echo -e "${j} x ${i} = $[i*j]\t\c" done echo done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/99.sh 1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 =81 1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49 1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36 1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 1 x 2 = 2 2 x 2 = 4 1 x 1 = 1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/99.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/99.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=1;i<=9;i++));do for((j=1;j<=$i;j++));do echo -en "${i}x${j}=$[i*j]\t" done echo done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/99.sh 1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
8>.打印国际象棋棋盘
[root@node101.yinzhengjie.org.cn ~]# cat shell/chess.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/chess.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 {1..8};do for j in {1..8};do flag=$[(j+i)%2] if [ $flag -eq 0 ];then echo -e "\033[47m \033[0m\c" else echo -e " \c" fi done echo done [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/chess.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/chess.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 {1..8};do for j in {1..4};do if [ $[i%2] -eq 0 ];then echo -e "\033[47m \033[0m \c" else echo -e " \033[47m \033[0m\c" fi done echo done [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/chess.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/chess.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 {1..4};do for j in {1..4};do echo -e "\033[47m \033[0m \c" done echo for k in {1..4};do echo -e " \033[047m \033[0m\c" done echo done [root@node101.yinzhengjie.org.cn ~]#
9>.编写脚本可以指定用户名的前缀以及创建用户的个数和密码
[root@node101.yinzhengjie.org.cn ~]# cat shell/user.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #FileName: shell/user.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 username(default "yinzhengjie"):" -t 30 username read -p "Please input the number of users:" -t 30 UsersNumber read -p "Please input the password of users:" -t 30 password if [ -z $username ] then username=yinzhengjie fi if [ ! -z "$username" -a ! -z "$UsersNumber" -a ! -z "$password" ] then y=$(echo $UsersNumber|sed 's/[0-9]//g') if [ -z "$y" ] then for (( i=1;i<=$UsersNumber;i=i+1 )) do useradd "$username$i" &>/dev/null echo $password | passwd --stdin "$username$i" &> /dev/null done fi fi [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bash shell/user.sh Please input username(default yinzhengjie):jason Please input the number of users:30 Please input the password of users:123456 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd | grep jason jason:x:1002:1002::/home/jason:/bin/bash jason1:x:1003:1003::/home/jason1:/bin/bash jason2:x:1004:1004::/home/jason2:/bin/bash jason3:x:1005:1005::/home/jason3:/bin/bash jason4:x:1006:1006::/home/jason4:/bin/bash jason5:x:1007:1007::/home/jason5:/bin/bash jason6:x:1008:1008::/home/jason6:/bin/bash jason7:x:1009:1009::/home/jason7:/bin/bash jason8:x:1010:1010::/home/jason8:/bin/bash jason9:x:1011:1011::/home/jason9:/bin/bash jason10:x:1012:1012::/home/jason10:/bin/bash jason11:x:1013:1013::/home/jason11:/bin/bash jason12:x:1014:1014::/home/jason12:/bin/bash jason13:x:1015:1015::/home/jason13:/bin/bash jason14:x:1016:1016::/home/jason14:/bin/bash jason15:x:1017:1017::/home/jason15:/bin/bash jason16:x:1018:1018::/home/jason16:/bin/bash jason17:x:1019:1019::/home/jason17:/bin/bash jason18:x:1020:1020::/home/jason18:/bin/bash jason19:x:1021:1021::/home/jason19:/bin/bash jason20:x:1022:1022::/home/jason20:/bin/bash jason21:x:1023:1023::/home/jason21:/bin/bash jason22:x:1024:1024::/home/jason22:/bin/bash jason23:x:1025:1025::/home/jason23:/bin/bash jason24:x:1026:1026::/home/jason24:/bin/bash jason25:x:1027:1027::/home/jason25:/bin/bash jason26:x:1028:1028::/home/jason26:/bin/bash jason27:x:1029:1029::/home/jason27:/bin/bash jason28:x:1030:1030::/home/jason28:/bin/bash jason29:x:1031:1031::/home/jason29:/bin/bash jason30:x:1032:1032::/home/jason30:/bin/bash [root@node101.yinzhengjie.org.cn ~]#
10>.添加10个用户user1-user10,密码为8位随机字符
[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c8 #获取8个随机字符方法 oVAsKdef [root@node101.yinzhengjie.org.cn ~]#
11>./etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
12>.编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
13>.计算100以内所有能被3整除的整数之和
14>.在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
15>.判断/var/目录下所有文件的类型
16>.给定一个IP地址和子网掩码,请编写脚本自动算出你给出IP地址的网络id
待整理笔记:
一.为什么要使用shell函数
一.为什么要使用shell函数
简单的说函数的作用就是把程序里多次调用的相同的代码部分定义成一份,然后起个名字,所有的调用都只用这个名字就可以了。修改代码时,只需要改变函数体内的代码即可
1 a>.把相同的代码定义成函数,可以减少程序代码量;
2 b>.增加程序的可读,易读性;
3 c>.实现程序的功能模块化;
二.shell语法格式
1 function 函数名() {
2 源代码
3 return
4 }
三.shell函数的执行
1 调用函数
2 1>.直接执行函数名即可
3 函数名
4 注意:
5 a>.执行函数时,不要带小括号了;
6 b>.函数定义及函数体必须在要执行的函数名的前面定义,shell的执行从上到下按行执行的;
7 2>.带参数的函数执行方法
8 函数名 参数1 参数2 .....
9 注意:
10 a>.在函数体中位置参数($1,$2,$3,...,$#,$*,$?以及$@)都可以是函数的参数;
11 b>.父脚本的参数则临时的被函数参数所掩盖或隐藏;
12 c>.$0比较特殊,他仍然是父脚本的名称;
13 d>.当函数完成时,原来的命令行参数会恢复;
14 e>.在shell函数里面。return命令的功能与工作方式与exit相同,用于跳出函数;
15 f>.在shell函数体里面使用exit会终止整个shell脚本;
16 g>.return语句会返回一个退出值给调用的程序;
四.案例展示
1.调用函数
1 [root@yinzhengjie shell]# more function1.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:y1053419035@qq.com
6
7 function yinzhengjie(){
8 echo "尹正杰到此一游!"
9 }
10 yinzhengjie
11 [root@yinzhengjie shell]#
12 [root@yinzhengjie shell]# sh function1.sh
13 尹正杰到此一游!
14 [root@yinzhengjie shell]#
2.导入函数
1 [root@yinzhengjie shell]# pwd
2 /root/yinzhengjie/shell
3 [root@yinzhengjie shell]#
4 [root@yinzhengjie shell]# more function1.sh
5 #!/bin/bash
6 #@author :yinzhengjie
7 #blog:http://www.cnblogs.com/yinzhengjie
8 #EMAIL:y1053419035@qq.com
9
10 function yinzhengjie(){
11 echo "尹正杰到此一游!"
12 }
13 yinzhengjie
14 [root@yinzhengjie shell]#
15 [root@yinzhengjie shell]# more function2.sh
16 #!/bin/bash
17 #@author :yinzhengjie
18 #blog:http://www.cnblogs.com/yinzhengjie
19 #EMAIL:y1053419035@qq.com
20
21 #如果存在“function1.sh”这个文件,就导入它,注意导入可以用"."
22 [ -f /root/yinzhengjie/shell/function1.sh ]&& . /root/yinzhengjie/shell/function1.sh || exit 100
23
24 yinzhengjie
25 [root@yinzhengjie shell]#
26 [root@yinzhengjie shell]# sh function2.sh
27 尹正杰到此一游!
28 尹正杰到此一游!
29 [root@yinzhengjie shell]#
3.函数传参
1 [root@yinzhengjie shell]# more function3.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:y1053419035@qq.com
6
7 function yinzhengjie(){
8 echo "Welcome to beijing,[$1]."
9 }
10 [root@yinzhengjie shell]#
11 [root@yinzhengjie shell]#
12 [root@yinzhengjie shell]#
13 [root@yinzhengjie shell]# more function4.sh
14 #!/bin/bash
15 #@author :yinzhengjie
16 #blog:http://www.cnblogs.com/yinzhengjie
17 #EMAIL:y1053419035@qq.com
18
19 #导入函数
20 . /root/yinzhengjie/shell/function3.sh
21 yinzhengjie 尹正杰
22
23 [root@yinzhengjie shell]#
24 [root@yinzhengjie shell]#
25 [root@yinzhengjie shell]# sh function4.sh
26 Welcome to beijing,[尹正杰].
27 [root@yinzhengjie shell]#
28 [root@yinzhengjie shell]#
4.应用案例
1 [root@yinzhengjie shell]# more CheckWeb.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:y1053419035@qq.com
6
7 function CheckUrl(){
8 curl -I -s $1 | head -1 && return 0 || return 100
9 }
10
11 CheckUrl $1
12 [root@yinzhengjie shell]#
13 [root@yinzhengjie shell]# sh CheckWeb.sh www.baidu.com
14 HTTP/1.1 200 OK
15 [root@yinzhengjie shell]#
16 [root@yinzhengjie shell]#
五.数组介绍
简单的说,数组就是相同数据类型的元素按一定顺序排列的集合。数组就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合。这个名字成为数组名,编号成为数组下标。组成数组的各个变量成为数组的分量,也成为数组的元素,有时也成为下标变量。
六.数组的定义以及基本使用
1>.声明数组
1 [root@yinzhengjie ~]# yinzhengjie=(100 200 300) #定义数组
2 [root@yinzhengjie ~]# echo ${#yinzhengjie[@]} #查看数组长度方式一
3 3
4 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]} #查看数组长度方式二
5 3
6 [root@yinzhengjie ~]#
7 [root@yinzhengjie ~]# echo ${yinzhengjie[0]} #查看某个数组
8 100
9 [root@yinzhengjie ~]# echo ${yinzhengjie[*]} #查看整个数组的元素
10 100 200 300
11 [root@yinzhengjie ~]#
2>.数组添加新元素
1 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
2 3
3 [root@yinzhengjie ~]#
4 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
5 100 200 300
6 [root@yinzhengjie ~]#
7 [root@yinzhengjie ~]# yinzhengjie[3]=jie
8 [root@yinzhengjie ~]#
9 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
10 100 200 300 jie
11 [root@yinzhengjie ~]#
3>.数组的修改
a>.基于下标的形式修改;
1 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
2 4
3 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
4 100 200 300 jie
5 [root@yinzhengjie ~]# yinzhengjie[0]=yin
6 [root@yinzhengjie ~]# yinzhengjie[1]=zheng
7 [root@yinzhengjie ~]#
8 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
9 yin zheng 300 jie
10 [root@yinzhengjie ~]#
b>.重新赋值方式修改
1 [root@yinzhengjie ~]# yinzhengjie=(100 200 300 400 500 600)
2 [root@yinzhengjie ~]# echo ${#yinzhengjie[@]}
3 6
4 [root@yinzhengjie ~]# echo ${yinzhengjie[@]}
5 100 200 300 400 500 600
6 [root@yinzhengjie ~]#
7 [root@yinzhengjie ~]# a=(${yinzhengjie[@]/500/5})
8 [root@yinzhengjie ~]# echo ${a[@]}
9 100 200 300 400 5 600
10 [root@yinzhengjie ~]# echo ${yinzhengjie[@]}
11 100 200 300 400 500 600
12 [root@yinzhengjie ~]#
4>.数组的删除
1 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
2 4
3 [root@yinzhengjie ~]#
4 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
5 yin zheng 300 jie
6 [root@yinzhengjie ~]# unset yinzhengjie[2]
7 [root@yinzhengjie ~]#
8 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}
9 3
10 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
11 yin zheng jie
12 [root@yinzhengjie ~]#
13 [root@yinzhengjie ~]# unset yinzhengjie
14 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
15
16 [root@yinzhengjie ~]#
5>.数组的截取
1 [root@yinzhengjie ~]# yinzhengjie=(100 200 300 400 500 600)
2 [root@yinzhengjie ~]# echo ${#yinzhengjie[@]}
3 6
4 [root@yinzhengjie ~]# echo ${yinzhengjie[@]}
5 100 200 300 400 500 600
6 [root@yinzhengjie ~]# echo ${yinzhengjie[@]:1:3}
7 200 300 400
8 [root@yinzhengjie ~]# echo ${yinzhengjie[@]:3:2}
9 400 500
10 [root@yinzhengjie ~]#
七.应用案例
1.案例一:把命令结果作为数组元素。
1 [root@yinzhengjie shell]# yinzhengjie=($(ls))
2 [root@yinzhengjie shell]# ls | wc -l
3 17
4 [root@yinzhengjie shell]# for((i=0;i<${#yinzhengjie[@]};i++));do echo ${yinzhengjie[$i]};done
5 argv1.sh
6 argv2.sh
7 argv3.sh
8 CheckWeb.sh
9 file_backup.sh
10 function1.sh
11 function2.sh
12 function3.sh
13 function4.sh
14 ls.log
15 partitions.sh
16 read.sh
17 res.txt
18 Tetris.sh
19 until.sh
20 while.sh
21 yinzhengjie.sh
22 [root@yinzhengjie shell]#
23 [root@yinzhengjie shell]# for file in ${yinzhengjie[@]};do echo $file;done
24 argv1.sh
25 argv2.sh
26 argv3.sh
27 CheckWeb.sh
28 file_backup.sh
29 function1.sh
30 function2.sh
31 function3.sh
32 function4.sh
33 ls.log
34 partitions.sh
35 read.sh
36 res.txt
37 Tetris.sh
38 until.sh
39 while.sh
40 yinzhengjie.sh
41 [root@yinzhengjie shell]#
2.用法展示
1 [root@yinzhengjie shell]# more array.sh
2 #!/bin/bash
3 #@author :yinzhengjie
4 #blog:http://www.cnblogs.com/yinzhengjie
5 #EMAIL:y1053419035@qq.com
6
7 array=(
8 yinzhengjie
9 bingan
10 alex
11 mage
12 oldboy
13 brother
14 )
15
16
17 for ((i=0;i<${#array[*]};i++))
18 do
19 echo "This is num [$i],then content is ${array[$i]}"
20 done
21
22
23 echo -----------------line------------------------
24
25
26 for name in ${array[@]}
27 do
28 echo "The boy is [$name]"
29 done
30 [root@yinzhengjie shell]#
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。
欢迎交流学习技术交流,个人微信: "JasonYin2020"(添加时请备注来源及意图备注)
作者: 尹正杰, 博客: https://www.cnblogs.com/yinzhengjie/p/7684782.html