shell函数与数组

              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 . Exit Status:
Returns the status of the last command executed.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# help for

1>.语法格式一
for 变量 in 值1 值2 值3 ...
do
源代码
done

也可以写成一行,案例如下:
  [root@node101.yinzhengjie.org.cn ~]# for i in {..};do let sum1+=i;done;echo sum=$sum1;
  sum=
  [root@node101.yinzhengjie.org.cn ~]#
2>.语法格式二(类似C语言风格的变量操作)
for (( 初始值;循环控制条件;变量变化 ))
do
源代码
done 也可以写成一行,案例如下:
  [root@node101.yinzhengjie.org.cn ~]# for((sum=,i=;i<=;i++));do let sum2+=i;done;echo sum=$sum2
  sum=
  [root@node101.yinzhengjie.org.cn ~]#

3>.for循环执行机制

  依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束
 
二.列表生成方式
1>.直接给出列表
[root@node101.yinzhengjie.org.cn ~]# cat shell/list1.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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= for i in a b c
do
echo "arg $count is $i"
count=count+
done
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list1.sh
arg is
arg is a
arg is
arg is b
arg is
arg is c
arg is
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# bash shell/list1.sh

2>.整数列表
[root@node101.yinzhengjie.org.cn ~]# cat shell/list2.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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= for i in {A..z}
do
echo "arg $count is $i"
count=count+
done
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list2.sh
arg is A
arg is B
arg is C
arg is D
arg is E
arg is F
arg is G
arg is H
arg is I
arg is J
arg is K
arg is L
arg is M
arg is N
arg is O
arg is P
arg is Q
arg is R
arg is S
arg is T
arg is U
arg is V
arg is W
arg is X
arg is Y
arg is Z
arg is [
arg is
arg is ]
arg is ^
arg is _
arg is `
arg is a
arg is b
arg is c
arg is d
arg is e
arg is f
arg is g
arg is h
arg is i
arg is j
arg is k
arg is l
arg is m
arg is n
arg is o
arg is p
arg is q
arg is r
arg is s
arg is t
arg is u
arg is v
arg is w
arg is x
arg is y
arg is z
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# bash shell/list2.sh

3>.返回列表的命令
[root@node101.yinzhengjie.org.cn ~]# cat shell/list3.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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= for i in `seq `
do
echo "arg $count is $i"
count=count+
done
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list3.sh
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
arg is
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# bash shell/list3.sh

4>.使用glob,如:"*.sh"
[root@node101.yinzhengjie.org.cn ~]# cat shell/list4.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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= 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 is /etc/profile.d/256term.sh
arg is /etc/profile.d/bash_completion.sh
arg is /etc/profile.d/colorgrep.sh
arg is /etc/profile.d/colorls.sh
arg is /etc/profile.d/crond.sh
arg is /etc/profile.d/lang.sh
arg is /etc/profile.d/less.sh
arg is /etc/profile.d/qt-graphicssystem.sh
arg is /etc/profile.d/qt.sh
arg is /etc/profile.d/vim.sh
arg is /etc/profile.d/which2.sh
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# bash shell/list4.sh

5>.位置变量引用
[root@node101.yinzhengjie.org.cn ~]# cat shell/list5.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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= for i in $@
do
echo "arg $count is $i"
count=count+
done
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/list5.sh python java golang shell c++ php
arg is python
arg is java
arg is golang
arg is shell
arg is c++
arg is php
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# bash shell/list5.sh python java golang shell c++ php

 
.请用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:
#Date: --
#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= for i in {..};do
let sum=sum+i
done echo "1到100的和为: $sum"
[root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh
1到100的和为:
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

[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:
#Date: --
#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= for i in {..};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的和为:
[root@node101.yinzhengjie.org.cn ~]#

参考案例2

[root@node101.yinzhengjie.org.cn ~]# vim shell/sum.sh
[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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= for i in {..};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的和为:
[root@node101.yinzhengjie.org.cn ~]#

参考案例3

[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:
#Date: --
#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= for i in {..};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的和为:
[root@node101.yinzhengjie.org.cn ~]#

参考案例4

[root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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=
read -t -p "Please enter the start number>>> " StartNumber
read -t -p "Please enter an end number>>> " EndNumber for ((i=$StartNumber;i<=$EndNumber;i=i+))
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>>>
Please enter an end number>>>
从1加到100的总和是:
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例5

2>.猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?
[root@node101.yinzhengjie.org.cn ~]# cat shell/monkey.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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=
for i in {..}
do
let sum=(sum+)*
done
echo "桃子的个数是: $sum"
unset sum
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh
桃子的个数是:
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

[root@node101.yinzhengjie.org.cn ~]# cat shell/monkey.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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- for i in `seq $day`
do
let sum=(sum+)*
done
echo "桃子的个数是: $sum"
unset sum
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh
请输入天数:
请输入最后一天剩余个数:
桃子的个数是:
[root@node101.yinzhengjie.org.cn ~]# bash shell/monkey.sh
请输入天数:
请输入最后一天剩余个数:
桃子的个数是:
[root@node101.yinzhengjie.org.cn ~]#

参考案例2

3>.将一个目录的所有文件后缀都更改为"*.log"
[root@node101.yinzhengjie.org.cn ~]# ll ~/backup/
total
-rw-r--r-- root root Nov : .pdf
-rw-r--r-- root root Nov : aa.png
-rw-r--r-- root root Nov : a.b.c.txt
-rw-r--r-- root root Nov : a.txt
-rw-r--r-- root root Nov : b1.rmvb
-rw-r--r-- root root Nov : b.jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/rename.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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 ~]#

参考案例1

[root@node101.yinzhengjie.org.cn ~]# cat shell/rename.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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
-rw-r--r-- root root Nov : .pdf
-rw-r--r-- root root Nov : aa.png
-rw-r--r-- root root Nov : a.b.c.txt
-rw-r--r-- root root Nov : a.txt
-rw-r--r-- root root Nov : b1.rmvb
-rw-r--r-- root root Nov : 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
-rw-r--r-- root root Nov : .log
-rw-r--r-- root root Nov : aa.log
-rw-r--r-- root root Nov : a.b.c.log
-rw-r--r-- root root Nov : a.log
-rw-r--r-- root root Nov : b1.log
-rw-r--r-- root root Nov : b.log
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例2

4>.编写脚本,提示请输入网络地址,如192.168.1.0,判断输入的网段中主机在线状态
[root@node101.yinzhengjie.org.cn ~]# cat shell/scanip.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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. for id in {..}
do
#并行执行下面的ping操作
{
if ping -c -w $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 ~]#

参考案例1

[root@node101.yinzhengjie.org.cn ~]# cat shell/scanip.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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 {..}
do
#并行执行下面的ping操作
{
if ping -c -w $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.
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 ~]#

参考案例2

5>.让用户输入行数和列数,编写脚本自动打印该矩形
[root@node101.yinzhengjie.org.cn ~]# cat shell/rectangle.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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:
Please input line:
**********
**********
**********
**********
**********
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

[root@node101.yinzhengjie.org.cn ~]# cat shell/rectangle.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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%+]
#输出时使用上面获取的随机颜色并高亮
#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:
Please input line:
**********
**********
**********
**********
**********
[root@node101.yinzhengjie.org.cn ~]#

参考案例2

[root@node101.yinzhengjie.org.cn ~]# cat shell/rectangle.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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 -o $i -eq $line -o $j -eq -o $j -eq $col ];then
#定义随机颜色
COLOR=$[RANDOM%+]
#输出时使用上面获取的随机颜色并高亮且附带闪烁功能
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:
Please input line:
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
[root@node101.yinzhengjie.org.cn ~]#

参考案例3

[root@node101.yinzhengjie.org.cn ~]# cat shell/rectangle.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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
|$line)
#定义随机颜色
COLOR=$[RANDOM%+]
#输出时使用上面获取的随机颜色并高亮且附带闪烁功能
echo -e "\033[1;5;${COLOR}m*\033[0m\c"
;;
*)
case $j in
|$col)
COLOR=$[RANDOM%+]
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:
Please input line:
**********
**********
**********
**********
**********
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例4

6>.打印等腰三角形
[root@node101.yinzhengjie.org.cn ~]# cat shell/triangle.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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*-
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 :
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/triangle.sh
Please input line :
*
***
*****
*******
*********
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

7>.打印九九乘法表
[root@node101.yinzhengjie.org.cn ~]# cat shell/.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/.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 {..};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/.sh
x =
x = x =
x = x = x =
x = x = x = x =
x = x = x = x = x =
x = x = x = x = x = x =
x = x = x = x = x = x = x =
x = x = x = x = x = x = x = x =
x = x = x = x = x = x = x = x = x =
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

[root@node101.yinzhengjie.org.cn ~]# cat shell/.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/.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=
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/.sh
请输入您想要打印的乘法表行数:
x =
x = x =
x = x = x =
x = x = x = x =
x = x = x = x = x =
x = x = x = x = x = x =
x = x = x = x = x = x = x =
x = x = x = x = x = x = x = x =
[root@node101.yinzhengjie.org.cn ~]#

参考案例2

[root@node101.yinzhengjie.org.cn ~]# cat shell/.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/.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 {..};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/.sh
x = x = x = x = x = x = x = x = x =
x = x = x = x = x = x = x = x =
x = x = x = x = x = x = x =
x = x = x = x = x = x =
x = x = x = x = x =
x = x = x = x =
x = x = x =
x = x =
x =
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例3

[root@node101.yinzhengjie.org.cn ~]# cat shell/.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: shell/.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=;i<=;i++));do
for((j=;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/.sh
1x1=
2x1= 2x2=
3x1= 3x2= 3x3=
4x1= 4x2= 4x3= 4x4=
5x1= 5x2= 5x3= 5x4= 5x5=
6x1= 6x2= 6x3= 6x4= 6x5= 6x6=
7x1= 7x2= 7x3= 7x4= 7x5= 7x6= 7x7=
8x1= 8x2= 8x3= 8x4= 8x5= 8x6= 8x7= 8x8=
9x1= 9x2= 9x3= 9x4= 9x5= 9x6= 9x7= 9x8= 9x9=
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

参考案例4

8>.打印国际象棋棋盘
[root@node101.yinzhengjie.org.cn ~]# cat shell/chess.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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 {..};do
for j in {..};do
flag=$[(j+i)%]
if [ $flag -eq ];then
echo -e "\033[47m \033[0m\c"
else
echo -e " \c"
fi
done
echo
done
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

[root@node101.yinzhengjie.org.cn ~]# cat shell/chess.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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 {..};do
for j in {..};do
if [ $[i%] -eq ];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 ~]#

参考案例2

[root@node101.yinzhengjie.org.cn ~]# cat shell/chess.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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 {..};do
for j in {..};do
echo -e "\033[47m \033[0m \c"
done
echo
for k in {..};do
echo -e " \033[047m \033[0m\c"
done
echo
done
[root@node101.yinzhengjie.org.cn ~]#

参考案例3

shell函数与数组

9>.编写脚本可以指定用户名的前缀以及创建用户的个数和密码
[root@node101.yinzhengjie.org.cn ~]# cat shell/user.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#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 username
read -p "Please input the number of users:" -t UsersNumber
read -p "Please input the password of users:" -t 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=;i<=$UsersNumber;i=i+ ))
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:
Please input the password of users:
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd | grep jason
jason:x::::/home/jason:/bin/bash
jason1:x::::/home/jason1:/bin/bash
jason2:x::::/home/jason2:/bin/bash
jason3:x::::/home/jason3:/bin/bash
jason4:x::::/home/jason4:/bin/bash
jason5:x::::/home/jason5:/bin/bash
jason6:x::::/home/jason6:/bin/bash
jason7:x::::/home/jason7:/bin/bash
jason8:x::::/home/jason8:/bin/bash
jason9:x::::/home/jason9:/bin/bash
jason10:x::::/home/jason10:/bin/bash
jason11:x::::/home/jason11:/bin/bash
jason12:x::::/home/jason12:/bin/bash
jason13:x::::/home/jason13:/bin/bash
jason14:x::::/home/jason14:/bin/bash
jason15:x::::/home/jason15:/bin/bash
jason16:x::::/home/jason16:/bin/bash
jason17:x::::/home/jason17:/bin/bash
jason18:x::::/home/jason18:/bin/bash
jason19:x::::/home/jason19:/bin/bash
jason20:x::::/home/jason20:/bin/bash
jason21:x::::/home/jason21:/bin/bash
jason22:x::::/home/jason22:/bin/bash
jason23:x::::/home/jason23:/bin/bash
jason24:x::::/home/jason24:/bin/bash
jason25:x::::/home/jason25:/bin/bash
jason26:x::::/home/jason26:/bin/bash
jason27:x::::/home/jason27:/bin/bash
jason28:x::::/home/jason28:/bin/bash
jason29:x::::/home/jason29:/bin/bash
jason30:x::::/home/jason30:/bin/bash
[root@node101.yinzhengjie.org.cn ~]#

参考案例1

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 ~]#

小提示:获取8个随机字符方法

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函数
  简单的说函数的作用就是把程序里多次调用的相同的代码部分定义成一份,然后起个名字,所有的调用都只用这个名字就可以了。修改代码时,只需要改变函数体内的代码即可
 a>.把相同的代码定义成函数,可以减少程序代码量;
b>.增加程序的可读,易读性;
c>.实现程序的功能模块化;
二.shell语法格式
 function 函数名() {
源代码
return
}
三.shell函数的执行
 调用函数
>.直接执行函数名即可
函数名
注意:
a>.执行函数时,不要带小括号了;
b>.函数定义及函数体必须在要执行的函数名的前面定义,shell的执行从上到下按行执行的;
>.带参数的函数执行方法
函数名 参数1 参数2 .....
注意:
a>.在函数体中位置参数($,$,$,...,$#,$*,$?以及$@)都可以是函数的参数;
b>.父脚本的参数则临时的被函数参数所掩盖或隐藏;
c>.$0比较特殊,他仍然是父脚本的名称;
d>.当函数完成时,原来的命令行参数会恢复;
e>.在shell函数里面。return命令的功能与工作方式与exit相同,用于跳出函数;
f>.在shell函数体里面使用exit会终止整个shell脚本;
g>.return语句会返回一个退出值给调用的程序;
 
四.案例展示
1.调用函数
 [root@yinzhengjie shell]# more function1.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com function yinzhengjie(){
echo "尹正杰到此一游!"
}
yinzhengjie
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# sh function1.sh
尹正杰到此一游!
[root@yinzhengjie shell]#
 
2.导入函数
 [root@yinzhengjie shell]# pwd
/root/yinzhengjie/shell
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# more function1.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com function yinzhengjie(){
echo "尹正杰到此一游!"
}
yinzhengjie
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# more function2.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com #如果存在“function1.sh”这个文件,就导入它,注意导入可以用"."
[ -f /root/yinzhengjie/shell/function1.sh ]&& . /root/yinzhengjie/shell/function1.sh || exit yinzhengjie
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# sh function2.sh
尹正杰到此一游!
尹正杰到此一游!
[root@yinzhengjie shell]#
 
3.函数传参
 [root@yinzhengjie shell]# more function3.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com function yinzhengjie(){
echo "Welcome to beijing,[$1]."
}
[root@yinzhengjie shell]#
[root@yinzhengjie shell]#
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# more function4.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com #导入函数
. /root/yinzhengjie/shell/function3.sh
yinzhengjie 尹正杰 [root@yinzhengjie shell]#
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# sh function4.sh
Welcome to beijing,[尹正杰].
[root@yinzhengjie shell]#
[root@yinzhengjie shell]#
4.应用案例
 [root@yinzhengjie shell]# more CheckWeb.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com function CheckUrl(){
curl -I -s $ | head - && return || return
} CheckUrl $
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# sh CheckWeb.sh www.baidu.com
HTTP/1.1 OK
[root@yinzhengjie shell]#
[root@yinzhengjie shell]#

五.数组介绍

  简单的说,数组就是相同数据类型的元素按一定顺序排列的集合。数组就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合。这个名字成为数组名,编号成为数组下标。组成数组的各个变量成为数组的分量,也成为数组的元素,有时也成为下标变量。
 
六.数组的定义以及基本使用
1>.声明数组
 [root@yinzhengjie ~]# yinzhengjie=(  )           #定义数组
[root@yinzhengjie ~]# echo ${#yinzhengjie[@]} #查看数组长度方式一 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]} #查看数组长度方式二 [root@yinzhengjie ~]#
[root@yinzhengjie ~]# echo ${yinzhengjie[]} #查看某个数组 [root@yinzhengjie ~]# echo ${yinzhengjie[*]} #查看整个数组的元素 [root@yinzhengjie ~]#
2>.数组添加新元素
 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}

 [root@yinzhengjie ~]#
[root@yinzhengjie ~]# echo ${yinzhengjie[*]} [root@yinzhengjie ~]#
[root@yinzhengjie ~]# yinzhengjie[]=jie
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# echo ${yinzhengjie[*]}
jie
[root@yinzhengjie ~]#
 
3>.数组的修改
a>.基于下标的形式修改;
 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}

 [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
jie
[root@yinzhengjie ~]# yinzhengjie[]=yin
[root@yinzhengjie ~]# yinzhengjie[]=zheng
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# echo ${yinzhengjie[*]}
yin zheng jie
[root@yinzhengjie ~]#
b>.重新赋值方式修改
 [root@yinzhengjie ~]# yinzhengjie=(     )
[root@yinzhengjie ~]# echo ${#yinzhengjie[@]} [root@yinzhengjie ~]# echo ${yinzhengjie[@]} [root@yinzhengjie ~]#
[root@yinzhengjie ~]# a=(${yinzhengjie[@]//})
[root@yinzhengjie ~]# echo ${a[@]} [root@yinzhengjie ~]# echo ${yinzhengjie[@]} [root@yinzhengjie ~]#
 
4>.数组的删除
 [root@yinzhengjie ~]# echo ${#yinzhengjie[*]}

 [root@yinzhengjie ~]#
[root@yinzhengjie ~]# echo ${yinzhengjie[*]}
yin zheng jie
[root@yinzhengjie ~]# unset yinzhengjie[]
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# echo ${#yinzhengjie[*]} [root@yinzhengjie ~]# echo ${yinzhengjie[*]}
yin zheng jie
[root@yinzhengjie ~]#
[root@yinzhengjie ~]# unset yinzhengjie
[root@yinzhengjie ~]# echo ${yinzhengjie[*]} [root@yinzhengjie ~]#
5>.数组的截取
 [root@yinzhengjie ~]# yinzhengjie=(     )
[root@yinzhengjie ~]# echo ${#yinzhengjie[@]} [root@yinzhengjie ~]# echo ${yinzhengjie[@]} [root@yinzhengjie ~]# echo ${yinzhengjie[@]::} [root@yinzhengjie ~]# echo ${yinzhengjie[@]::} [root@yinzhengjie ~]#
七.应用案例
1.案例一:把命令结果作为数组元素。
 [root@yinzhengjie shell]# yinzhengjie=($(ls))
[root@yinzhengjie shell]# ls | wc -l [root@yinzhengjie shell]# for((i=;i<${#yinzhengjie[@]};i++));do echo ${yinzhengjie[$i]};done
argv1.sh
argv2.sh
argv3.sh
CheckWeb.sh
file_backup.sh
function1.sh
function2.sh
function3.sh
function4.sh
ls.log
partitions.sh
read.sh
res.txt
Tetris.sh
until.sh
while.sh
yinzhengjie.sh
[root@yinzhengjie shell]#
[root@yinzhengjie shell]# for file in ${yinzhengjie[@]};do echo $file;done
argv1.sh
argv2.sh
argv3.sh
CheckWeb.sh
file_backup.sh
function1.sh
function2.sh
function3.sh
function4.sh
ls.log
partitions.sh
read.sh
res.txt
Tetris.sh
until.sh
while.sh
yinzhengjie.sh
[root@yinzhengjie shell]#
2.用法展示
 [root@yinzhengjie shell]# more array.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com array=(
yinzhengjie
bingan
alex
mage
oldboy
brother
) for ((i=;i<${#array[*]};i++))
do
echo "This is num [$i],then content is ${array[$i]}"
done echo -----------------line------------------------ for name in ${array[@]}
do
echo "The boy is [$name]"
done
[root@yinzhengjie shell]#
 
 
上一篇:scrapy的基础概念和流程


下一篇:数据库--SQL