Today to share a recent study in the script script ideas and simple programming cycle.
A programming logic processing:
- Order execution
- Choose Execute
- Cycle execution
1. for loop
for the variable name in the list; do
loop
DONE
for circulation list generating method
is as follows:
in both the list of a few cycles i is assigned to several cycles performed several times
seq reverse skip list is generated
in double quotes parameter will become a whole
"$ *" will become an integral parameter both abc
"$ @" will display parameters to distinguish both abc
careful to use
Look at the specific usage and script it
- 100 odd accumulated within
- For both modulo 21 = + command executes
#!/bin/bash declare -i sum=0 for i in {1..100};do if [ $[i%2] -eq 1 ];then let sum+=i fi done echo sum=$sum```
- 10 users create randomly generated password and change password at next logon
#!/bin/bash for i in {1..10};do useradd user$i password=`tr -dc '[:alnum:]' < /dev/urandom |head -c 8` echo user$i:$password | tee -a pass.txt |chpasswd passwd -e user$i echo "User:user$i is created" done
4. Extract the user name and batch delete users
5. Scan the network to find the IP can Unicom and saved in a file
NETID = 172.16.128#!/bin/bash for HOSTID in {1..254};do { if ping -c1 -W1 $NETID.$HOSTID &> /dev/null ;then echo $NETID.$HOSTID is up | tee -a hostlist.txt fi } & done wait```
The second cycle of writing 2.for
for ((initialization of control variables; conditional expressions; correction manipulated variable expression))
do
loop
done
as shown above, by the expression 1 and expression to two judgments, as is true on the execution command, then the expression 3 and then the generation of two expression determination, until the expression is not satisfied with the second condition, the loop stops
to see an example:
this is a diagram showing the effect of formulas Jiujiuchengfa FIG following#!/bin/bash<br/>for ((i=1;i<=9;i++));do<br/>for((j=1;j<=i;j++));do<br/>echo -e "${j}x$i=$[j*i]\t\c"<br/>done<br/>echo <br/>done
two methods for the multiplication table showing 99
3.while cycle
while CONDITION; do
循环体
done
CONDITION:循环控制条件;进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”,则执行一次循环;直到条件测试状态为“false”终止循环
1.取10个随机数字 取出最大值和最小值
```#!/bin/bash
i=1
MAX=$RANDOM
MIN=$MAX
echo $MAX
while [ $i -lt 10 ];do
N=$RANDOM
echo $N
if [ $N -gt $MAX ];then
MAX=$N
elif [ $N -lt $MIN ];then
MIN=$N
fi
let i++
done
echo MAX=$MAX, MIN=$MIN2. 监控服务的启动状态、如果关闭则重新服务 ![](https://s1.51cto.com/images/blog/201906/18/6257148a3ab81965bbf3f55629e25ecb.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=) 每10秒监控一次,并生成日志文件 ```SLEEPTIME=10 while :;do if ! killall -0 php-fpm &> /dev/null;then systemctl restart php-fpm &> /dev/null echo "At `date +'%F %T'` php-fpm is restarted" >> /var/log/monitor.log fi sleep $SLEEPTIME done```
4.循环控制语句continue
用于循环体中
continue [N]:提前结束第N层的本轮循环,而直接进入下一轮判断;最内层为第1层
continue 中断循环中某一次循环 continue2 终端上一层循环
5.循环控制语句break
用于循环体中
break [N]:提前结束第N层循环,最内层为第1层
同理,break2 中断第二层循环
6.接下来找几个有趣的例子;
- 数字猜猜猜
#!/bin/bash N=$[RANDOM%10+1] while read -p "input a number(1-10):" NUM ;do if [[ $NUM =~ ^[[:digit:]]+$ ]];then if [ $NUM -eq $N ];then echo OK break elif [ $NUM -gt $N ];then echo too large! else echo too small! fi else echo "Please input a digit" fi done```
- Monkey off his first day of a number of peach, half eaten immediately, not addiction, but also to eat the next morning a turn eaten by the remaining peach half, then eat one. After the morning before the rest of the day eat half a zero. When the morning of the 10th day want to eat, only a peach. Seeking first day were picked How much?
#!/bin/bash<br/>sum=1<br/>for i in {1..9};do<br/>let sum=(sum+1)*2<br/>done<br/>echo "num is : $sum "
- Print isosceles triangle
``` #!/bin/bash
read -p "place input isoceles triangle:" line
for i in $(seq 1 $line);do
spacenum=$[line-i]
anum=$[2i-1]
for j in $(seq 1 $spacenum);do
echo -e " \c"
done
for k in $(seq 1 $anum);do
echo -e "\c"
done
echo
done
4. 扫描网络中有多少主机是开启,有多少主机是关闭
``` #!/bin/sh
read -p "please input new(eg:192.168.0.0): " net
netid=`echo $net|cut -d. -f1-2`
i=0
up=0
down=0
while [ $i -le 254 ];do
j=1
while [ $j -le 254 ];do
if ping -c1 -w1 $netid.$i.$j &>/dev/null;then
echo "the $netid.$i.$j is up"
let up++
else
echo "the $netid.$i.$j is down"
let down++
fi
let j++
done
let i++
done
echo "the up is $up"
echo "the down is $down"```
5. 实现打印国际象棋棋盘
```#!/bin/bash
i=1
while [ $i -le 8 ];do
j=1
while [ $j -le 4 ];do
if [ $[$i%2] -eq 0 ];then
echo -e "\e[1;41m \e[0m\e[1;43m \e[0m\c"
else
echo -e "\e[1;43m \e[0m\e[1;41m \e[0m\c"
fi
let j++
done
echo
let i++
done```
for 的循环写法
``` #!/bin/bash
for i in {1..8};do
for j in {1..4};do
if [ $[i%2] -eq 0 ];then
echo -e "\e[1;41m \e[0m\e[1;43m \e[0m\c"
else
echo -e "\e[1;43m \e[0m\e[1;41m \e[0m\c"
fi
done
echo
done```
## 二.函数
函数由两部分组成:函数名和函数体
help function
语法一:
f_name (){
...函数体...
}
语法二:
function f_name {
...函数体...
}
语法三:
function f_name () {
...函数体...
}
通常用第三种方法定义,比较清楚明了
函数获取操作系统版本
func_os_version(){
sed -nr 's/.* ([0-9]+)\..*/\1/p' /etc/redhat-release
}
#### 删除shell函数
命令格式为:
unset function_name
示例:
unset findit
再键入set命令,函数将不再显示
环境函数
使子进程也可使用
声明:export -f function_name
查看:export -f 或 declare -xf
#### 通常在较本中常用函数会放在单独文件中
使用时可以调用函数库
函数文件已创建好后,要将它载入shell
定位函数文件并载入shell的格式
. filename 或 source filename
注意:此即<点> <空格> <文件名>
这里的文件名要带正确路径
判断是否是数字的shell函数
```func_is_digit(){
if [ ! "$1" ];then
echo "Usage:func_is_digit number"
return 10
elif [[ $1 =~ ^[[:digit:]]+$ ]];then
return 0
else
echo "Not a digit"
return 1
fi
}```
## 今天就分享到这里了 谢谢咯
Reproduced in: https: //blog.51cto.com/14322554/2410511