小榄网站,怎么制作网站站内链接,网站建设哪个比较好,做网站青岛本篇文章继续给大家介绍Shell编程#xff0c;包括for循环、并发问题#xff0c;while循环#xff0c;流程控制语句#xff0c;函数传参、函数变量、函数返回值#xff0c;反向破解MD5等内容。
1.for循环
for 变量 in [取值列表] 取值列表可以是数字 字符串 变量 序列…本篇文章继续给大家介绍Shell编程包括for循环、并发问题while循环流程控制语句函数传参、函数变量、函数返回值反向破解MD5等内容。
1.for循环
for 变量 in [取值列表] 取值列表可以是数字 字符串 变量 序列 命令
do for循环将取到的值以此赋值给变量命令即可
done[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in a b c
doecho $i
done
[rootweb01 Day04]# sh test.sh
a
b
c#也可以在命令行中写for循环
[rootweb01 Day04]# for i in seq 10;do echo $i;done
1
2
3
4
5
6
7
8
9
10[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in a b c
doecho $i
done
[rootweb01 Day04]# sh test.sh
a b
c[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in 10 20 30
doecho $i
done
[rootweb01 Day04]# sh test.sh
10
20
30[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in {1..5}
doecho $i
done
[rootweb01 Day04]# sh test.sh
1
2
3
4
5[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in {a..d}
doecho $i
done
[rootweb01 Day04]# sh test.sh
a
b
c
d
1探测10.0.0.1-10.0.0.254哪些ip在线ping的通说明在线
[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in {1..254}
doIP10.0.0.$iping -c1 -W1 $IP /dev/nullif [ $? -eq 0 ];thenecho $IP 在线fi
done
[rootweb01 Day04]# sh test.sh
10.0.0.1 在线
10.0.0.2 在线
10.0.0.4 在线
10.0.0.7 在线
10.0.0.8 在线
10.0.0.31 在线
10.0.0.51 在线
10.0.0.61 在线#可以再连接下Xshell过滤下ping
[rootweb01 ~]# ps axu|grep ping
root 13760 0.0 0.1 128552 1272 pts/0 T 09:25 0:00 ping -c1 -W1 10.0.0.107
root 13897 0.0 0.1 128552 1268 pts/0 S 09:28 0:00 ping -c1 -W1 10.0.0.109
root 13899 0.0 0.0 112808 964 pts/2 S 09:28 0:00 grep --colorauto ping
这样速度慢我们可以用花括号括住循环体后面再加实现并发注意并不是所有的循环都使用并发快如果循环次数过大的情况下并发多了会过多的占用资源不利于处理循环体的数据有些时候甚至比不并发还要慢。
[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in {1..256}
do{IP10.0.0.$iping -c1 -W1 $IP /dev/nullif [ $? -eq 0 ];thenecho $IP 在线fi}
done
echo 在线取IP完成......[rootweb01 Day04]# sh test.sh #由于是并发所以出现了顺序错乱谁先ping好就先返回谁
10.0.0.4 在线
10.0.0.2 在线
10.0.0.31 在线
10.0.0.1 在线
10.0.0.8 在线
10.0.0.7 在线
10.0.0.51 在线
10.0.0.61 在线
在线取IP完成......#用另一个Xshell过滤可以看到有很多进程
[rootweb01 ~]# ps axu|grep ping
root 13760 0.0 0.1 128552 1272 pts/0 T 09:25 0:00 ping -c1 -W1 10.0.0.107
root 13945 0.0 0.1 128552 1272 pts/0 T 09:29 0:00 ping -c1 -W1 10.0.0.155
root 13976 0.0 0.1 128552 1276 pts/0 T 09:30 0:00 ping -c1 -W1 10.0.0.26
root 14004 0.0 0.1 128552 1268 pts/0 R 09:30 0:00 ping -c1 -W1 10.0.0.10
root 14005 0.0 0.1 128552 1272 pts/0 R 09:30 0:00 ping -c1 -W1 10.0.0.5
root 14007 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.15
root 14008 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.16
root 14009 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.14
root 14010 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.12
root 14011 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.13
root 14012 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.17
root 14020 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.18
root 14040 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.19
root 14041 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.22
root 14042 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.24
root 14043 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.23
root 14044 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.20
root 14045 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.21
root 14075 0.0 0.1 128552 1276 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.33
root 14076 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.34
root 14077 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.36
root 14078 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.39
root 14079 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.42
root 14080 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.43
root 14081 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.32
root 14082 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.28
root 14083 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.41
root 14084 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.25
root 14085 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.26
root 14086 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.27
root 14087 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.30
root 14088 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.35
root 14089 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.37
root 14090 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.40
root 14091 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.29
root 14092 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.38
root 14093 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.69
root 14094 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.59
root 14095 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.68
root 14097 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.48
root 14098 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.50
root 14099 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.56
root 14100 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.62
root 14101 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.63
root 14102 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.53
root 14103 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.44
root 14104 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.52
root 14105 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.54
root 14106 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.58
root 14107 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.64
root 14108 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.70
root 14109 0.0 0.1 128552 1276 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.67
root 14110 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.55
root 14112 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.45
root 14113 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.46
root 14114 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.47
root 14115 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.49
root 14116 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.57
root 14117 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.60
root 14118 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.66
root 14119 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.71
root 14120 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.65
root 14126 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.74
root 14127 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.72
root 14128 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.73
root 14129 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.76
root 14130 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.75
root 14136 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.77
root 14142 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.78
root 14143 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.80
root 14144 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.79
root 14145 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.81
root 14146 1.0 0.1 128552 1276 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.83
root 14147 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.84
root 14148 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.85
root 14149 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.86
root 14150 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.82
root 14156 0.0 0.1 128552 1276 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.87
root 14157 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.88
root 14158 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.89
root 14159 0.0 0.1 128552 1276 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.90
root 14160 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.91
root 14170 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.93
root 14171 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.94
root 14172 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.95
root 14173 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.92
root 14175 0.0 0.0 112812 964 pts/2 S 09:30 0:00 grep --colorauto ping
root 14176 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.97
root 14177 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.98
root 14178 0.0 0.1 128552 1268 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.96
root 14179 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.99
root 14180 0.0 0.1 128552 1276 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.100
root 14186 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.101
root 14187 0.0 0.1 128552 1276 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.103
root 14188 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.105
root 14189 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.104
root 14190 0.0 0.1 128552 1272 pts/0 S 09:30 0:00 ping -c1 -W1 10.0.0.102
并发还可能出现并发的内容没返回但是脚本后面的语句已经执行完毕的情况这种情况下对于有逻辑关系的语句不利所以使用并发的时候需要考虑这种情况这时候我们可以使用wait解决这个问题。
#并发导致逻辑出问题
[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in {1..256}
do{IP10.0.0.$iping -c2 -W2 $IP /dev/nullif [ $? -eq 0 ];thenecho $IP 在线fi}
done
echo 在线取IP完成......
[rootweb01 Day04]# sh test.sh
在线取IP完成......
[rootweb01 Day04]# 10.0.0.7 在线
10.0.0.4 在线
10.0.0.8 在线
10.0.0.1 在线
10.0.0.2 在线
10.0.0.31 在线
10.0.0.51 在线
10.0.0.61 在线[rootweb01 Day04]##使用wait控制逻辑
[rootweb01 Day04]# cat test.sh
#!/bin/bash
for i in {1..256}
do{IP10.0.0.$iping -c2 -W2 $IP /dev/nullif [ $? -eq 0 ];thenecho $IP 在线fi}
done
wait
echo 在线取IP完成......
[rootweb01 Day04]# sh test.sh
10.0.0.31 在线
10.0.0.2 在线
10.0.0.1 在线
10.0.0.7 在线
10.0.0.4 在线
10.0.0.8 在线
10.0.0.51 在线
10.0.0.61 在线
在线取IP完成......
2从1加到100
[rootweb01 Day04]# cat 100.sh
#!/bin/bash
sum0
for i in seq 100
do sumecho $i$sum|bc
done
echo $sum
[rootweb01 Day04]# sh 100.sh
5050#用命令也可以
[rootweb01 Day04]# seq -s 100|bc #-s是用什么分割
5050 399乘法表
[rootweb01 Day04]# cat 99x.sh
#!/bin/bash
for i in seq 9
dofor b in seq $idoprintf $b x $i $[i*b]\t #printf是格式化输出doneprintf \n
done
[rootweb01 Day04]# sh 99x.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 4for循环读取文件默认以空格取值
[rootweb01 Day04]# cat for_file.sh
#!/bin/bash
for i in cat /etc/hosts
doecho $i
done
[rootweb01 Day04]# sh for_file.sh
127.0.0.1
localhost
localhost.localdomain
localhost4
localhost4.localdomain4
::1
localhost
localhost.localdomain
localhost6
localhost6.localdomain6[rootweb01 Day04]# cat user.txt
zhangsan
lisi
wangwu
[rootweb01 Day04]# cat for_file.sh
#!/bin/bash
for i in cat /server/scripts/Day04/user.txt
douseradd $i
done
[rootweb01 Day04]# sh for_file.sh
[rootweb01 Day04]# tail -3 /etc/passwd
zhangsan:x:1007:1007::/home/zhangsan:/bin/bash
lisi:x:1008:1008::/home/lisi:/bin/bash
wangwu:x:1009:1009::/home/wangwu:/bin/bash
5批量创建删除用户
[rootweb01 Day04]# cat yonghu.sh
#!/bin/bash
read -p 请输入用户的前缀: qianzhui
if ! [[ $qianzhui ~ ^[a-z] ]];thenecho 请注意前缀输入格式 exit
fi
read -p 请输入要操作的数量: num1
if ! [[ $num1 ~ ^[0-9]$ ]];thenecho 请注意输入数量的格式exit
fi
echo 为您生成如下用户
shuchu
for i1 in $(seq $num1)
doshuchu$shuchu $qianzhui$i1
done
echo $shuchu
read -p 您可以选择操作y为创建d为删除i为查看用户id[y|d|i] caozuo
if [ $caozuo y ];thenfor i2 in $(seq $num1)do id $qianzhui$i2 /dev/nullif [ $? -eq 0 ];thenecho $user 已存在elseuseradd $qianzhui$i2if [ $? -eq 0 ];thenecho $user 创建成功fifidone
elif [ $caozuo d ];thenfor i3 in $(seq $num1)doid $qianzhui$i3 /dev/nullif [ $? -ne 0 ];thenecho $user 不存在不需要删除exitelseuserdel -rf $qianzhui$i3if [ $? -eq 0 ];thenecho $user 删除成功fifidone
elif [ $caozuo i ];thenfor i4 in $(seq $num1)doid $qianzhui$i4doneecho 用户查询完毕
else echo 请输入正确的内容[y|d|i]
fi
[rootweb01 Day04]# sh yonghu.sh
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1 qwer2 qwer3 qwer4 qwer5
您可以选择操作y为创建d为删除i为查看用户id[y|d|i]d不存在不需要删除
[rootweb01 Day04]# sh yonghu.sh
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1 qwer2 qwer3 qwer4 qwer5
您可以选择操作y为创建d为删除i为查看用户id[y|d|i]y创建成功创建成功创建成功创建成功创建成功
[rootweb01 Day04]# sh yonghu.sh
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1 qwer2 qwer3 qwer4 qwer5
您可以选择操作y为创建d为删除i为查看用户id[y|d|i]i uid1010(qwer1) gid1010(qwer1) groups1010(qwer1)
uid1011(qwer2) gid1011(qwer2) groups1011(qwer2)
uid1012(qwer3) gid1012(qwer3) groups1012(qwer3)
uid1013(qwer4) gid1013(qwer4) groups1013(qwer4)
uid1014(qwer5) gid1014(qwer5) groups1014(qwer5)
用户查询完毕
[rootweb01 Day04]# sh yonghu.sh
请输入用户的前缀: qwer
请输入要操作的数量: 5
为您生成如下用户
qwer1 qwer2 qwer3 qwer4 qwer5
您可以选择操作y为创建d为删除i为查看用户id[y|d|i]d删除成功删除成功删除成功删除成功删除成功
6创建user1-user10共10个用户只有user5不创建家目录不允许登入
[rootweb01 Day04]# cat yonghu2.sh
#!/bin/bash
for i in seq 10
doif [ $i 5 ];thenuseradd user$i -M -s /sbin/nologinelse useradd user$ifi
done
[rootweb01 Day04]# sh yonghu2.sh
[rootweb01 Day04]# tail -10 /etc/passwd
user1:x:1013:1013::/home/user1:/bin/bash
user2:x:1014:1014::/home/user2:/bin/bash
user3:x:1015:1015::/home/user3:/bin/bash
user4:x:1016:1016::/home/user4:/bin/bash
user5:x:1017:1017::/home/user5:/sbin/nologin
user6:x:1018:1018::/home/user6:/bin/bash
user7:x:1019:1019::/home/user7:/bin/bash
user8:x:1020:1020::/home/user8:/bin/bash
user9:x:1021:1021::/home/user9:/bin/bash
user10:x:1022:1022::/home/user10:/bin/bash
[rootweb01 Day04]# ls /home/|grep user
user1
user10
user2
user3
user4
user6
user7
user8
user9
[rootweb01 Day04]# su - user5
su: warning: cannot change directory to /home/user5: No such file or directory
This account is currently not available.
2.while循环
while [条件表达式] #表达式成立则执行不成立不执行
do可执行命令
done#死循环里面如果有read -p就会卡住
while true
doecho hehe
donewhile [ -f /etc/passwd ]
doecho hehe
done#循环1-100
i1
while [ $i -le 100 ]
doecho $ilet i
done
1while从1加到100
[rootweb01 Day04]# cat test.sh
#!/bin/bash
i1
while [ $i -le 100 ]
dosum$[sumi]let i
done
echo $sum
[rootweb01 Day04]# sh test.sh
5050 2while读取文件按照行读取内容
用for也能做用while方便些
[rootweb01 Day04]# cat yonghu.txt
zs 123
ls 456
lw 789
[rootweb01 Day04]# cat duqu.sh
while read line
douserecho $line|awk {print $1}passecho $line|awk {print $2}useradd $userecho $pass|passwd --stdin $user
doneyonghu.txt
[rootweb01 Day04]# sh duqu.sh
Changing password for user zs.
passwd: all authentication tokens updated successfully.
Changing password for user ls.
passwd: all authentication tokens updated successfully.
Changing password for user lw.
passwd: all authentication tokens updated successfully.
[rootweb01 Day04]# su - zs
[zsweb01 ~]$ su - ls
Password:
[lsweb01 ~]$ exit
logout
[zsweb01 ~]$ exit
logout
用for来操作需要新设个变量除以2判断奇数还是偶数奇数是用户名偶数是密码
[rootweb01 Day04]# cat duqu_for.sh
#!/bin/bash
for i in cat yonghu.txt
do let qreecho $q%2|bcif [ $re 1 ];thenuser$iuseradd $userelsepass$iecho $pass|passwd --stdin $userfi
done
[rootweb01 Day04]# sh duqu_for.sh
Changing password for user zs.
passwd: all authentication tokens updated successfully.
Changing password for user ls.
passwd: all authentication tokens updated successfully.
Changing password for user lw.
passwd: all authentication tokens updated successfully.
[rootweb01 Day04]# tail -3 /etc/passwd
zs:x:1023:1023::/home/zs:/bin/bash
ls:x:1024:1024::/home/ls:/bin/bash
lw:x:1025:1025::/home/lw:/bin/bash
3while统计行数
[rootweb01 Day04]# cat hangshu.sh
#!/bin/bash
wenjian/etc/passwd
while read line
dolet i
done$wenjian
echo $wenjian 中总共 $i 行
[rootweb01 Day04]# sh hangshu.sh
/etc/passwd 中总共 34 行
3. 流程控制语句
exitbreakcontinueexit 退出脚本break 跳出循环continue 忽略当前剩余代码从头继续执行
1exit
[rootweb01 Day04]# cat liucheng.sh
#!/bin/bash
while true
doecho 1exitecho 2
done
echo 3
[rootweb01 Day04]# sh liucheng.sh
1
2break
[rootweb01 Day04]# cat liucheng.sh
#!/bin/bash
while true
doecho 1breakecho 2
done
echo 3
[rootweb01 Day04]# sh liucheng.sh
1
3
3continue
[rootweb01 Day04]# cat liucheng.sh
#!/bin/bash
while true
doecho 1continueecho 2
done
echo 3
[rootweb01 Day04]# sh liucheng.sh
1
1
1
......#continu使用示例
[rootweb01 Day04]# cat liucheng.sh
#!/bin/bash
while true
doread -p 请输入密码: passif ! [ $pass 123456 ];thenecho 密码输入错误continueelseecho 密码输入正确break;fi
done
echo 登录成功
[rootweb01 Day04]# sh liucheng.sh
请输入密码: 1
密码输入错误
请输入密码: 123456
密码输入正确
登录成功
4.函数
1、完成特定功能的代码块2、可以复用3、函数类似变量先定义再调用区别是变量不调用也会执行但是函数不调用不执行
1函数定义
[rootweb01 Day04]# cat fun.sh
#!/bin/bash
fun1(){echo 第一种函数定义方法
}
function fun2 { #注意不加括号有空格echo 第二种函数定义方法
}
function fun3(){echo 第三种函数定义方法
}
fun1
fun2
fun3
[rootweb01 Day04]# sh fun.sh
第一种函数定义方法
第二种函数定义方法
第三种函数定义方法
与变量不同函数不调用不执行变量不调用也执行
[rootweb01 Day04]# cat fun.sh
#!/bin/bash
namekoten
fun1(){echo 第一种函数定义方法
}
function fun2 {echo 第二种函数定义方法
}
function fun3(){echo 第三种函数定义方法
}
[rootweb01 Day04]# sh -x fun.shnamekoten
想在当前shell执行直接source脚本或者. 脚本即可跟变量同理
[rootweb01 Day04]# source fun.sh
[rootweb01 Day04]# echo $name
koten
[rootweb01 Day04]# fun1
第一种函数定义方法
2函数传参
与脚本传参不同函数中不能直接接受shell的传参
错误方式
[rootweb01 Day04]# cat chuancan.sh
#!/bin/bash
fun1(){if [ -f $1 ];thenecho 文件存在else echo 文件不存在fi
}
fun1
[rootweb01 Day04]# sh chuancan.sh /etc/passwd #虽然显示存在
文件存在
[rootweb01 Day04]# sh -x chuancan.sh /etc/passwd #但是看流程函数中并没有显示fun1[ -f ]echo 文件存在
文件存在
正确用法直接写到调用名称后面
[rootweb01 Day04]# cat chuancan.sh
#!/bin/bash
fun1(){if [ -f $1 ];thenecho 文件存在else echo 文件不存在fi
}
fun1 $1
[rootweb01 Day04]# sh chuancan.sh /etc/passwd
文件存在
注意区分脚本中的参数与函数中的参数
[rootweb01 Day04]# cat chuancan.sh
#!/bin/bash
fun1(){if [ -f $1 ];then #这个是调用函数的传参echo $1 文件存在else echo $1 文件不存在fi
}
fun1 $2 $1 #这个是执行脚本时候的传参
[rootweb01 Day04]# sh chuancan.sh /etc/hosts /etc/passwd
/etc/passwd 文件存在
也可以通过变量的方式传参一开始定义好变量
[rootweb01 Day04]# cat chuancan.sh
#!/bin/bash
file$1
fun1(){if [ -f $file ];thenecho $file 文件存在else echo $file 文件不存在fi
}
fun1
[rootweb01 Day04]# sh chuancan.sh /etc/passwd
/etc/passwd 文件存在
3函数变量
函数中可以调用shell脚本的变量
在函数中定义的变量可以只在函数体中生效在shell中不生效
#正常是在函数体内外都会生效
[rootweb01 Day04]# cat bianliang.sh
#!/bin/bash
fun1(){namebossecho $name
}
fun1
echo $name
[rootweb01 Day04]# sh bianliang.sh
boss
boss#只在函数体中生效
[rootweb01 Day04]# cat bianliang.sh
#!/bin/bash
fun1(){local namebossecho $name
}
fun1
echo $name
[rootweb01 Day04]# sh bianliang.sh
boss[rootweb01 Day04]#
4函数返回值
exit和return都可以定义
[rootweb01 Day04]# cat fanhuizhi.sh
#!/bin/bash
if [ -f $1 ];thenecho $1 存在exit 100
else echo $1 不存在exit 50
fi
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwd
/etc/passwd 存在
[rootweb01 Day04]# echo $?
100[rootweb01 Day04]# cat fanhuizhi.sh
#!/bin/bash
fun1(){if [ -f $1 ];thenecho 存在 return 100elseecho 不存在return 99fi
}
fun1 $?
echo $?
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwd
不存在
99
使用返回值的时候容易出错我们要时刻注意返回值是否有因为执行了新的命令而刷新
如下所示的错误写法
[rootweb01 Day04]# cat fanhuizhi.sh
#!/bin/bash
fun1(){
if [ -f $1 ];thenreturn 100
else return 50
fi
}
fun1 $1
[ $? -eq 50 ] echo 文件不存在
[ $? -eq 100 ] echo 文件存在
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwdd
文件不存在
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwd
[rootweb01 Day04]#
由于在执行完函数后又执行了$?与50的判断所以判断后$?刷新成了其他数值所以永远不会echo出文件存在这种情况一般有两种解决办法。
[rootweb01 Day04]# cat fanhuizhi.sh
#!/bin/bash
fun1(){
if [ -f $1 ];thenreturn 100
else return 50
fi
}
fun1 $1
[ $? -eq 50 ] echo 文件不存在
echo $?
[ $? -eq 100 ] echo 文件存在
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwd
1
一种是将两个条件表达式写成一个在使用函数的返回值之前不执行其他命令。
[rootweb01 Day04]# cat fanhuizhi.sh
#!/bin/bash
fun1(){
if [ -f $1 ];thenreturn 100
else return 50
fi
}
fun1 $1
[ $? -eq 50 ] echo 文件不存在 || echo 文件存在
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwd
文件存在
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwdddd
文件不存在
另一种也是系统中常做的操作就是当我们的函数体执行完毕后给一个变量后面用变量去判断这样$?有变化也没有关系反正变量不会有变化。
[rootweb01 Day04]# cat fanhuizhi.sh
#!/bin/bash
fun1(){[ -f $1 ];thenreturn 100
else return 50
fi
}
fun1 $1
re$?
[ $re -eq 50 ] echo 文件不存在
[ $re -eq 100 ] echo 文件存在
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwd
文件存在
[rootweb01 Day04]# sh fanhuizhi.sh /etc/passwdddd
文件不存在
5.反向破解MD5
下面是随机数的md5值4个取了前8位2两个取了前7位通过脚本反向破解出随机数
echo $((RANDOM))|md5sum|cut -c1-8
0b364f36
7f1e6feb
c5b795e2
5f8b9f68
echo $((RANDOM))|md5sum|cut -c1-7
081691c
76728eb
我们的思路就是利用for循环反向破解优化就是尽可能少的操作加快遍历速度更快的破解出来
[rootweb01 Day04]# cat fanxiang.sh
#!/bin/bash
for i in seq 32767
domd5echo $i|md5summd5_7echo $md5|cut -c1-7md5_8echo $md5|cut -c1-8if [ $md5_7 081691c ];thenecho $i $md5_7elif [ $md5_7 76728eb ];thenecho $i $md5_7elif [ $md5_8 0b364f36 ];thenecho $i $md5_8elif [ $md5_8 7f1e6feb ];thenecho $i $md5_8elif [ $md5_8 c5b795e2 ];thenecho $i $md5_8elif [ $md5_8 5f8b9f68 ];thenecho $i $md5_8 fi
done
[rootweb01 Day04]# sh fanxiang.sh
691 c5b795e2
5343 081691c
11902 7f1e6feb
21364 76728eb
25375 5f8b9f68
30458 0b364f36