-------------------------------------------------------------For练习--------------------------------------------------
1.遍历var下面所有文件,并判断类型;
#!/bin/bash
set -u
read -p "请输入需判断
for file in find $Dir
;do
if [ -e $file ];then
if [ -f $file ];then
echo “This $file is commondfile”
elif [ -d $file ];then
echo “Thie $file is directory”
elif [ -h $file ];then
echo “This $file is link”
elif [ -p $file ];then
echo “This $file is pipefile”
else
echo “This $file is socketfile”
fi
else
echo “/var下无文件”
fi
done
2.创建10个用户,密码为随机8位字符,并保存至/Date/passwd.txt中;
#!/bin/bash
#PS:创建10个用户,密码为8个随机字符
set -u
for Num in {1…10};do
useradd User
N
u
m
e
c
h
o
‘
h
e
a
d
−
c
4
/
d
e
v
/
r
a
n
d
o
m
∣
b
a
s
e
64
‘
∣
t
e
e
−
a
/
r
o
o
t
/
D
a
t
e
/
p
a
s
s
w
d
.
t
x
t
∣
p
a
s
s
w
d
−
−
s
t
d
i
n
U
s
e
r
Num echo `head -c 4 /dev/random |base64`|tee -a /root/Date/passwd.txt | passwd --stdin User
Numecho‘head−c4/dev/random∣base64‘∣tee−a/root/Date/passwd.txt∣passwd−−stdinUserNum
done
3.读取文件,并将K开头的行尾增加stop,S开头行行尾增加start;
#!/bin/bash
#PS:判断文件名称是否是K开头或者S开头,S开头加Start,K开头加Stop
set -u
cp -rp /etc/rc.d/rc3.d/ /root/Date/
cd /root/Date/rc3.d
for filename in ;do
if [[ $filename =~ 1. ]];then
mv $filename $filename-Stop
elif [[ $filename =~ 2.* ]];then
mv $filename $filename-Start
else
echo “没有K或者S开头的文件”
fi
done
- 输入整数n的值,计算1+2+…+n的总和
#!/bin/bash
#PS:输入整数n的值,计算1+2+…+n的总和
read -p “请输入求和的最大值:” Num
j=0
for (( i=i;i<=Num;i++ ));do
let j= j + j+ j+i
done
echo $j
5.计算100以内所有能被3整除的正整数的和;
#!/bin/bash
#PS:计算N以内所有能被3整除的正整数的和;
read -p “请输入需要求和的整数:” Num
N=0
for i in seq $Num
;do
j=$[i%3]
if [[
j
=
=
0
]
]
;
t
h
e
n
l
e
t
N
+
=
j == 0 ]];then let N+=
j==0]];thenletN+=i
fi
done
echo $N
6.判断网段内主机状态;
#!/bin/bash
#PS:判断网段内的主机在线状态;
read -p “请输入192.168.0.0:” IP
if [[
I
P
=
[
0
−
9
]
+
.
∗
.
[
0
]
IP =~ ^[0-9]+.*.[0]
IP= [0−9]+.∗.[0] ]];then
N0=echo $IP|awk -F"." '{print $1}'
N1=echo $IP|awk -F"." '{print $2}'
N2=echo $IP|awk -F"." '{print $3}'
if [[ $N1 == 0 ]];then
for i in {0…10};do
for j in {0…10};do
for n in {0…10};do
ping -w1
N
0.
N0.
N0.i.
j
.
j.
j.n > /dev/null && echo “host is active” || echo “host is passive”
[ $n -eq 10 ] && break
done
[ $j -eq 10 ] && break
done
[ $i -eq 10 ] && break
done
elif [[ $N2 == 0 ]];then
for i in {0…11};do
for j in {0…11};do
ping -w1
N
0.
N0.
N0.N1.
i
.
i.
i.j > /dev/null && echo “host is active” || echo “host is passive”
[ $j -eq 10 ] && break
done
[ $i -eq 10 ] && break
done
else
for i in {0…12};do
ping -w1
N
0.
N0.
N0.N1.
N
2.
N2.
N2.i > /dev/null && echo “host is active” || echo “host is passive”
[ $i -eq 10 ] && break
done
fi
else
echo “Please input Network segment:192.168.0.0”
fi
7.打印99乘法表
#!/bin/bash
#PS:打印99乘法表
set -u
for (( i=0;i<10;i++ )) ;do
for (( j=0;j<=i;j++ )) ;do
let Num=
i
∗
i*
i∗j
echo -e “
i
∗
i*
i∗j=$Num\t\c”
done
echo -e “”
done
- 在testdir目录下创建10个格式为数字N加八个字母的.html文件
#!/bin/bash
#PS:在testdir目录下创建10个格式为数字N加八个字母的.html文件
set -u
Dir=/root/Date/testdir
if [[ ! -d $Dir ]];then
mkdir -p /root/Date/testdir
else
for file in {1…10};do
touch D i r / Dir/ Dir/file-tr -dc "0-9" < /dev/urandom |head -c 8
.html
done
fi
9.直角三角形:
#!/bin/bash
#PS:打印直角三角形
read -p “请输入边长:” lang
for ((i=1;i<=$lang;i++));do
for ((j=1;j<=i;j++));do
echo -e “*\c”
done
echo -e “”
done
echo “---------------------------------------”
#PS:倒立直角三角形
for ((i=$lang;i>0;i–));do
for ((j=1;j<=i;j++));do
echo -e “*\c”
done
echo -e “”
done
echo “---------------------------------------”
#PS:等腰三角形
for ((i=1;i<=
l
a
n
g
;
i
+
+
)
)
;
d
o
f
o
r
(
(
j
=
lang;i++));do for ((j=
lang;i++));dofor((j=lang;j>i;j–));do
echo -n " "
done
for n in seq 1 $i
;do
echo -n " *"
done
for ((lang-1;lang<i;lang++));do
echo -n " *"
done
echo -e “”
done
echo “--------------------------------------”
#PS:倒立等腰三角形
for ((i=
l
a
n
g
;
i
>
0
;
i
−
−
)
)
;
d
o
f
o
r
(
(
s
=
1
;
s
<
=
lang;i>0;i--));do for ((s=1;s<=
lang;i>0;i−−));dofor((s=1;s<=lang-$i;s++));do
echo -n " "
done
for m in seq 1 $i
;do
echo -n "* "
done
echo -e “”
done
10.算数题
#!/bin/bash
#PS:猴子有一天去果园摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃了一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半另加一个。到第N天再想吃时,就只剩一个桃子了,还想再去摘,果农想知道猴子一共摘了多少桃子?
set -u
cat << EOF
猴子有一天去果园摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃>
子吃了一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半另加一个。到第十天再想吃时,就只剩一>个桃子了,一共多少桃子
EOF
read -p “请输入猴子一共偷了多少天?” Day
Sum=1
for i in seq $Day
;do
let Sum=(Sum+1)*2
done
echo $Sum
-------------------------------while练习题------------------------------------
- 每分钟检查分区利用率,若超过90%,则发送告警邮件
#!/bin/bash
#PS:每分钟检查分区利用率,若超过90%,则发送告警邮件
#配置发信邮箱
set from=1016368036@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1016368036@qq.com
set amtp-auth-passwork=xiangdeming@123
set smtp-auth=log
set ssl-verify=ignore
#防止空变量
set -u
#主机IP
Hostname=hostname -I |awk '{print $1}'
#循环
while true;do
USE=df |awk -F"[[:space:]]+|%" '$0 ~ "^/dev"{if (NR>2){print $5}}' |sort -rn
#遍历磁盘使用率,有Use大于90的就发邮件
for i in $USE;do
if [[ KaTeX parse error: Undefined control sequence: \/ at position 38: …df |sed -nr "/^\̲/̲dev/s/^([^ ]+).…i%.*/\1/p"`
echo “Disk will be full from H o s t n a m e , 磁 盘 : Hostname,磁盘: Hostname,磁盘:dev,利用率:$i” | mail -s “disk warning” 1016368036@qq.com
fi
done
sleep 60
done
1.求100以内所有正奇数的和;
#!/bin/bash
#PS=求100以内所有正奇数之和;
set -u
Sum=0
i=1
while [
i
−
l
e
100
]
;
d
o
l
e
t
X
=
i -le 100 ];do let X=
i−le100];doletX=i%2
if [
X
!
=
0
]
;
t
h
e
n
l
e
t
S
u
m
+
=
i
l
e
t
i
+
+
e
l
s
e
l
e
t
i
+
+
f
i
d
o
n
e
e
c
h
o
"
100
内
奇
整
数
的
和
为
“
X != 0 ];then let Sum+=i let i++ else let i++ fi done echo "100内奇整数的和为“
X!=0];thenletSum+=ileti++elseleti++fidoneecho"100内奇整数的和为“Sum”"
2,while写法:判断网段内的主机在线状态;
#!/bin/bash
#PS:判断网段内的主机在线状态;
set -u
read -p “请输入网段,如:192.168.0.0:” IP
if [[
I
P
=
[
0
−
9
]
+
.
∗
.
[
0
]
IP =~ ^[0-9]+.*.[0]
IP= [0−9]+.∗.[0] ]];then
N0=echo $IP|awk -F"." '{print $1}'
N1=echo $IP|awk -F"." '{print $2}'
N2=echo $IP|awk -F"." '{print $3}'
#类192.0.0.0网段
if [ $N1 == 0 ];then
i=0
while [ $i -le 254 ];do
j=0
while [ $j -le 254 ];do
n=0
while [ $n -le 254 ];do
ping -w1 -c1
N
0.
N0.
N0.i.
j
.
j.
j.n && echo “host is active” || echo “host is passive”
let n++
done
let j++
done
let i++
done
#类192.168.0.0网段:
elif [ $N2 == 0 ];then
j=0
while [ $j -le 254 ];do
n=0
while [ $n -le 254 ];do
ping -w1 -c1
N
0.
N0.
N0.N1.
j
.
j.
j.n && echo “host is active” || echo “host is passive”
let n++
done
let j++
done
#类192.168.213.0网段:
else
n=0
while [ $n -le 15 ];do
ping -w1 -c1
N
0.
N0.
N0.N1.
N
2.
N2.
N2.n && echo “host is active” || echo “host is passive”
let n++
[ $n == 5 ] && exit 4
done
fi
fi
3.九九乘法表:
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le
i
]
;
d
o
l
e
t
N
u
m
=
i ];do let Num=
i];doletNum=i*
j
e
c
h
o
−
e
"
j echo -e "
jecho−e"i*
j
=
j=
j=Num\t\c"
let j++
done
let i++
echo -e “”
done
- 利用变量RANDOM生成10个随机数字,输出这10个数字,并显示其中的最大值和最小值
#!/bin/bash
#PS:利用变量RANDOM生成10个随机数字,输出这10个数字,并显示其中的最大值和最小值;
i=0
max=echo $RANDOM
mini=$max
while [ $i -le 9 ];do
NUM=echo $RANDOM
echo $NUM >> /root/Random.list
if [ $NUM -gt $max ];then
max=echo $NUM
elif [ $NUM -lt $mini ];then
mini=echo $NUM
fi
let i++
done
echo "ALL number arecat /root/Random.list|tr -s "\n" " "
"
echo “MAX is $max,Min is $mini”
echo “” > /root/Random.list
#PS:使用数组方式实现
declare -i mini max #申明整数
declare -a nums #非关联数组
i=0
#循环
while [
i
−
l
e
10
]
;
d
o
n
u
m
s
[
i -le 10 ];do nums[
i−le10];donums[i]=$RANDOM
if [
i
−
e
q
0
]
;
t
h
e
n
m
i
n
i
=
i -eq 0 ];then mini=
i−eq0];thenmini={nums[0]}
max=${nums[0]}
elif [ KaTeX parse error: Expected '}', got 'EOF' at end of input: {nums[i]} -gt
m
a
x
]
;
t
h
e
n
m
a
x
=
max ];then max=
max];thenmax={nums[$i]}
else
[ KaTeX parse error: Expected '}', got 'EOF' at end of input: {nums[i]} -lt KaTeX parse error: Expected 'EOF', got '&' at position 8: mini ] &̲& mini={nums[$i]}
fi
let i++
done
echo “ALL number are ${nums[*]}”
echo “MAX is $max,Min is $mini”
-
打印国际象棋棋盘
#!/bin/bash
#PS:打印国际象棋棋盘
read -p “请输入棋盘大小:” line
#棋盘格1
func_1() {
echo -en ‘\033[47;30m \033[0m’
}
#棋盘格2
func_2() {
echo -en ‘\033[41;31m \033[0m’
}
#循环
for ((i=1;i<= l i n e ; i + + ) ) ; d o f o r ( ( j = 1 ; j < = line;i++));do for ((j=1;j<= line;i++));dofor((j=1;j<=line;j++));do
if [ ( ( (( ((j%2)) -eq ( ( (( ((i%2)) ];then
func_1
else
func_2
fi
done
echo “”
done -
破解echo $RANDOM|md5sum|cut -c1 -10的值
#!/bin/bash
#PS=破解echo KaTeX parse error: Expected 'EOF', got '#' at position 36: …1 -10的值 set -u #̲创建数组并赋值 read -p…num;i++));do
Nums[$i]=echo $RANDOM|md5sum|cut -c1-10
done
#数组长度
Conut=${#Nums[*]}
echo “ALL number are KaTeX parse error: Expected 'EOF', got '#' at position 12: {Nums[*]}" #̲RANDOM的范围为[0,32767]
for ((j=0;j<=32767;j++));do
md5=echo $j |md5sum|cut -c1-10
for Table inecho ${Nums[*]}
;do
if [[ $md5 =~ T a b l e ] ] ; t h e n e c h o " Table ]];then echo " Table]];thenecho"Table对应的值为$j”
#以变量Count作为计数器,值全部找到则跳出外层循环,否则结束当前内部循环;
let Conut–;[ $Count -eq 0 ] && break 2 ||break
fi
done
done
----------------------------------------综合练习1----------------------------- -
每3秒到系统上获取已登录的用户信息;如果发现用户hacker登录,则将登录时间和主机记录存放在日志/var/log/login.log中
#!/bin/bash
#PS:每3秒到系统上获取已登录的用户信息;如果发现用户hacker登录,则将登录时间和主机记录存放在日志/var/log/login.log中
set -u
while true;do
w|awk ‘NR>2{print $1,$4}’ |while read User Login;do
if [ $User =~ hacker ];then
echo $User $Login >> /var/log/login.log
fi
done
sleep 3
done
2.随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则推出;
#!/bin/bash
#PS:随机生成10以内的数字,并对比大小,给出大或小的提示,当大小相等时则输出游戏结束
declare -a num
while true;do
Num_1=echo $RANDOM |head -c1
Num_2=echo $RANDOM |head -c1
if [ $Num_1 -lt
N
u
m
2
]
;
t
h
e
n
e
c
h
o
"
N
u
m
1
=
Num_2 ];then echo "Num_1=
Num2];thenecho"Num1=Num_1,Num_2=$Num_2,Num_1小了"
elif [ $Num_1 -gt
N
u
m
2
]
;
t
h
e
n
e
c
h
o
"
N
u
m
1
=
Num_2 ];then echo "Num_1=
Num2];thenecho"Num1=Num_1,Num_2=
N
u
m
2
,
N
u
m
1
大
了
"
e
l
s
e
e
c
h
o
"
N
u
m
1
=
Num_2,Num_1大了" else echo "Num_1=
Num2,Num1大了"elseecho"Num1=Num_1,Num_2=$Num_2,数值相等,游戏技术"
break
fi
done
#PS:升级版:根据输入的值生成随机位数的值,并对比大小,给出大或小的提示,当大小相等时则输出游戏结束
declare -a Num
X=0
func_1() {
read -p “请输入想生成随机数值的位数(1-3):” A
case
A
i
n
1
∣
2
∣
3
)
C
o
u
n
t
=
A in 1|2|3) Count=
Ain1∣2∣3)Count=A
return $Count
;;
*)
echo “请输入1-3的数字;”
exit
;;
esac
}
func_2() {
for j in seq 2
;do
Num[$j]=(echo $RANDOM |head -c $Count)
done
return $Num
}
func_1
state=“true”
while $state;do
func_2
let X++
#Count=echo ${Num[*]}
if [ ${Num[1]} -gt
N
u
m
[
2
]
]
;
t
h
e
n
e
c
h
o
"
N
u
m
1
=
{Num[2]} ];then echo "Num_1=
Num[2]];thenecho"Num1={Num[1]},Num_2=${Num[2]},Num_1大了"
elif [ ${Num[1]} -lt
N
u
m
[
2
]
]
;
t
h
e
n
e
c
h
o
"
N
u
m
1
=
{Num[2]} ];then echo "Num_1=
Num[2]];thenecho"Num1={Num[1]},Num_2=
N
u
m
[
2
]
,
N
u
m
1
小
了
"
e
l
s
e
e
c
h
o
"
共
处
理
{Num[2]},Num_1小了" else echo "共处理
Num[2],Num1小了"elseecho"共处理X个数值"
echo “Num_1=
N
u
m
[
1
]
,
N
u
m
2
=
{Num[1]},Num_2=
Num[1],Num2={Num[2]},数值相等,游戏结束”
state=“false”
break 2
fi
done
- 以文件名作为参数,统计所有参数文件的总行数
#!/bin/bash
#PS:以文件名作为参数,统计所有参数文件的总行数
for i in $*;do
if [ -f $i ];then
A=cat $i |wc -l
let Count+=A
else
echo “文件不存在”
fi
done
echo “总行数为$Count”
#PS:升级版:以文件名作为参数,并同时输出文件名称和对应行数,最终输出总数;
for i in $*;do
if [ -f $i ];then
A=echo $i
B=cat $i |wc -l
echo “文件
A
的
行
数
为
A的行数为
A的行数为B”
let Count+=B
else
echo “文件不存在或不可读”
fi
done
echo “总行数为$Count”
- 用两个以上的数字作为参数。显示其中的最大值和最小值
#!/bin/bash
#PS:用两个以上的数字作为参数。显示其中的最大值和最小值
declare -a Num Odd Number
set -u
state=“true”
#引用位置参数,并建立数组
for (( i=1;i<=echo $#
;i++ ));do
if [ $(( i E v e n [ i%2 )) -eq 0 ];then Even[ iEven[i]=(eval echo '$'{$i})
else
Odd[ i ] = ‘ ( e v a l e c h o ′ i]=`(eval echo ' i]=‘(evalecho′’{$i})fi done #生成最大值与最小值 Even_Max=
echo ${Even[]}|tr " " “\n”|sort -nr|head -1Odd_Max=
echo ${Odd[]}|tr " " “\n”|sort -nr|head -1Even_Min=
echo ${Even[]}|tr " " “\n”|sort -nr|tail -1Odd_Min=
echo ${Odd[]}|tr " " “\n”|sort -nr|tail -1#确认最大值 if [ $Even_Max -gt $Odd_Max ];then echo "最大值为:$Even_Max" else echo "最大值为:$Odd_Max" fi #确认最小值 if [ $Even_Min -lt $Odd_Min ];then echo "最小值为:$Even_Min" else echo "最小值为:$Odd_Min" fi #输出表格头 awk 'BEGIN{print"|----------------数字清单---------------|"}' #输出清单 Number=
echo ${Even[]} ${Odd[]}|tr " " “\n”|sort -necho ${Number[@]}|awk -v Count=
echo KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲` '{for(i=1;i<=…i,$j}’
----------------------------函数练习-------------------------------
------------------------数组练习------------------------------------------
- 生成10个随机保存于数组中,并找出最大值和最小值;
#!/bin/bash
#PS=数组范例i;生成10个随机保存于数组中,并找出最大值和最小值;
#申明数组 (-i 整数)(-a 非关联数组)
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
#生成随机数
nums[ i ] = i]= i]=RANDOM
#判断$i是否等于0(第一个元素),是第一个元素则用来作为临时的最大值与最小值;
[ KaTeX parse error: Expected 'EOF', got '&' at position 12: i -eq 0 ] &̲& min={nums[0]} && max=${nums[0]} && continue
#判断当前数组内元素是否大于最大值,是的话替换元素值;
[ KaTeX parse error: Expected '}', got 'EOF' at end of input: {nums[i]} -gt KaTeX parse error: Expected 'EOF', got '&' at position 7: max ] &̲& max={nums[$i]} && continue
#判断当前数组内元素是否小于最小值,是的话替换元素值;
[ KaTeX parse error: Expected '}', got 'EOF' at end of input: {nums[i]} -lt KaTeX parse error: Expected 'EOF', got '&' at position 7: min ] &̲& min={nums[$i]}
done
echo “All number are ${nums[*]}”
echo “Max is $max”
echo “min is $min”