#####1、脚本中的函数
######变量:利用一串字符表示一个值
######函数:利用一串字符表示一个动作
######简化脚本内容,提高可读性,且可以循环调用
#######实验:使用函数循环建立、删除用户,exit退出
[root@desktop mnt]# vim user_ctrl.sh
#!/bin/bash
ACTION_ADD(){
[ "$1" = add ]&&{
read -p "Please input your user name: " NAME
read -p "Please input your user passwd: " -s PASSWD
useradd $NAME
echo $PASSWD | passwd --stdin $NAME
}
}
ACTION_DEL(){
[ "$1" = del ]&&{
read -p "Please input your user name: " NAME
userdel -r $NAME
}
}
USER_CTL()
{ read -p "Please input action: " ACTION
[ "$ACTION" = exit ]&&{
echo bye!
exit 0
}
ACTION_ADD $ACTION
ACTION_DEL $ACTION
USER_CTL
}
USER_CTL
#######测试:
######2、常用的语句
######exit直接退出
######break退出当前循环,不影响其他
######continue跳出本层循环,进入下一次循环
#######实验:输出除4外的1~10
[root@desktop mnt]# vim test.sh
#!/bin/bash
for NUM in `seq 1 10`
do
while [ "$NUM" != "4" ]
do
echo $NUM
break
done
done
######(1)for语句
######for
######do *********
######done
[root@desktop mnt]# vim for.sh
#!bin/bash
for NUM in `seq 1 2 10` //使用seq可以设置步长
do
echo $NUM
done
[root@desktop mnt]# vim for.sh
#!bin/bash
for NUM in {1..5} //相当于 `seq 1 5`
do
echo $NUM
done
[root@desktop mnt]# vim for.sh
#!/bin/bash
for((NUM=1;NUM<=5;NUM++)) //作用相同
do
echo $NUM
done
#######实验:显示教室中可连通的网络
[root@desktop mnt]# vim check_host.sh
#!/bin/bash
for IP in `seq 0 50`
do
ping -c1 -w1 172.25.254.$IP &> /dev/null && echo "172.25.254.$IP"
done
#######实验:备份数据库
[root@desktop mnt]# vim mysql_dump.sh
#!/bin/bash
DATABASE=`mysql -uroot -EN -e "show databases;"|grep -E "^\*|schema$" -v`
mkdir -p /mnt/mysql_dump
for DATABASE in $DATABASE
do
mysqldump -uroot $DATABASE > /mnt/mysql_dump/${DATABASE}.sql
[ "$?" -eq "0" ]&&{
echo -e "\033[32m$DATABASE is backuped!\033[0m"
}
done
#######测试:
[root@desktop mysql_backup]# sh mysql_dump.sh
linux is backuped!
mysql is backuped!
test is backuped!
westos is backuped!
######(2)while do语句
######while true
######do *********
######done
#######实验:当根分区使用量超过80%时,向root用户发送邮件警告
[root@desktop mnt]# vim root_check.sh
#!/bin/bash
COUNT=`df|awk -F " " '/\/$/{print $5}'|awk -F "%" '{print $1}'`
while true
do
[ "$COUNT" -ge "80" ]&&{
echo Your / is full!| mail -s warning root
}
sleep 1
done
#######测试:
######(3)if语句
######if
######***
######then
######***
######elif
######***
######then
######***
######else
######***
######if
#######实验:编写脚本判断文件类型
[root@desktop mnt]# vim file_check.sh
#!/bin/bash
Check_file()
{ if
[ "$1" "$2" ]
then
echo "$2" is $3
exit
fi
}
if
[ "$#" -ne "1" ]
then
echo -e "\033[31mPlease input a file name!\033[0m"
elif
[ ! -e "$1" ]
then
echo "$1 is not exist!"
else
Check_file -L $1 link
Check_file -f $1 "common file"
Check_file -S $1 socket
Check_file -b $1 block
Check_file -c $1 character
Check_file -d $1 directory
fi
#######测试:
#######实验:利用用户文件和密码文件建立用户,当文件数量不对报错,文件不存在时报错,文件行数差异时报错,用户存在时显示用户存在,不修改其密码,用户不存在时建立用户并设定密码
[root@desktop mnt]# vim user_create.sh
#!/bin/bash
if
[ "$#" -ne "2" ]
then
echo -e "\033[31mPlease input userfile and passfile!\033[0m"
exit 1
elif
[ ! -e "$1" ]
then
echo -e "\033[31m$1 is not exist!\033[0m"
exit 1
elif
[ ! -e "$2" ]
then
echo -e "\033[31m$2 is not exist!\033[0m"
exit 1
elif
USER_LINE=`awk 'BEGIN{N=0}//{N++}END{print N}' $1`
PASS_LINE=`awk 'BEGIN{N=0}//{N++}END{print N}' $2`
[ "$USER_LINE" -ne "$PASS_LINE" ]
then
echo -e "\033[31m The two file lines is different!\033[0m"
exit 1
else
for LINE_NUM in `seq 1 $USER_LINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $1`
PASSWORD=`sed -n "${LINE_NUM}p" $2`
useradd $USERNAME&&{
echo $PASSWORD | passwd --stdin $USERNAME
}
done
fi
#######测试:
######(4)case语句
主要用于字符匹配型问题,解决该类问题时,相比于if语句速度快
#######实验:输入dog时,输出cat;输入cat时,输出dog;其他输出error
[root@desktop mnt]# vim test.sh
#!/bin/bash
case $1 in
dog)
echo cat
;;
cat)
echo dog
;;
*)
echo error
esac
#######测试:
######(5)expect
#######实验:编写脚本,使用ssh直接连接其他主机
[root@desktop mnt]# yum install expect -y
[root@desktop mnt]# which expect
/usr/bin/expect
[root@desktop mnt]# vim auto_connect.exp
#!/usr/bin/expect
set timeout 5
set IP [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]
spawn ssh root@$IP
expect {
"connecting" { send "yes\r";exp_continue}
"password" { send "$PASS\r"}
}
interact
[root@desktop mnt]# chmod +x /mnt/auto_connect.exp
[root@desktop mnt]# /mnt/auto_connect.exp 172.25.254.167 redhat
#######测试: