1.循环打印数字1-5
for语句:
#!/bin/bash
for i in {1..5}
do
echo $i
done
while语句:
#!/bin/bash
i=i
while [ $i -le 5 ]
do
echo $i
let i=$i+1
done
until语句:
i=1
until [ $i -gt 5]
do
echo $i
let i++
done
2.Shell脚本同步系统时间
要求:写一个脚本,30秒同步一次系统时间,时间同步服务器(自己搭建)。如果同步失败,则进行邮件报警,每次失败都报警;同步成功,也进行邮件通知,但是成功100次才通知一次。
(1)安装并启动mail
1.install package:mailx,sendmail,postfix
2.start postfix service:systemctl start postfix;systemctl enable postfix
3.echo "system date sycess" | mail -s "time status" root
(2)编写脚本
#!/bin/bash
sucessCount=0
while true
do
echo "Checking system date ..."
chronyc sources | grep '^*' &> /dev/null
if [ $? -eq 0 ];then
let sucessCount++
if [ $sucessCount -eq 100 ]:then
echo "system date sucess" | mail -s "check system date" root
sucessCount=0
fi
else
echo "system date failed" | mail -s "check system date" root
sucessCount=0
fi
sleep 30
done
3.自动化搭建NFS服务器并共享目录
项目思路:
(1)测试网络是否畅通
(2)关闭Selinux和防火墙(或者设置Selinux和防火墙策略)
(3)确认软件是否安装(rpcbind,Redhat8自动安装)
(4)创建和发布共享目录(共享目录+共享网段+共享权限)
(5)启动服务并设置开机自启动
(6)搭建完成后提示:NFS共享服务已搭建完成,欢迎下次使用
客户端测试:
showmount -e IP
mount.nfs IP:/sharedir /your_dir
#!/bin/bash
echo '1. check network is ok?'
ping -c1 172.25.254.39 &> /dev/null
if [ $? -eq 0 ];then
echo "Network is ok"
else
echo "Network isn't ok"
exit 1
fi
echo "2. Check Selinux and Firewalld"
setenforce 0 &> /dev/null
echo "Selinux set Disable"
systemctl stop firewalld &> /dev/null
echo "Firewalld stop"
echo "3. Check rpcbind installed?"
rpm -q rpcbind &> /dev/null
if [ $? -eq 0 ];then
echo "Rpcbind has installed"
else
yum install rpcbind -y &> /dev/null && echo "Rpcbind install success" || echo "Rpcbind install failed";exit 1
fi
echo "4. Create Share Directory and Share it"
read -p "---please input share direcory:" dir
if [ -e $dir ];then
echo "Share Directory $dir exists"
else
mkdir -p $dir
echo " Share Directory $dir Create sucess"
fi
#All user can write,Only owner and root can delete.
chmod 1777 $dir
read -p "---please input share subnet: " subnet
read -p "---please input share premission(ro/rw): " permission
#Edit nfs configure file
#shareDir Subnet(Permission)
echo "Edit nfs configure file /etc/exports"
read -p "input 1 -> clear config,default is add:" choice
if [ $choice -eq 1 ];then
> /etc/exports
fi
cat >> /etc/exports <<EOF
$dir $subnet($permission)
EOF
echo "5. Start and enable service"
echo "Check nfs-server is start?"
systemctl status nfs-server.service | grep active &>/dev/null
if [ $? -eq 0 ];then
systemctl restart nfs-server
echo "Nfs reatart success"
else
systemctl start rpcbind
systemctl start nfs-server
systemctl enable rpcbind
systemctl enable nfs-server
fi
echo "6. Check Client can access share directory?"
showmount -e 172.25.254.39 | grep $dir &> /dev/null
[ $? -eq 0 ] && echo "Share success" || echo "Share failed";exit 1
echo "NFS共享服务已搭建完成,欢迎下次使用"
4.模拟一个多任务维护界面,当执行程序时先显示总菜单,然后进行选择后做相应的维护监控操作
#!/bin/bash
function menu(){
echo "
h 显示命令的帮助
f 显示磁盘分区
d 显示磁盘挂载
m 显示内存使用
u 显示系统负载
q 退出程序
"
}
menu
while true
do
read -p "请输入你要操作的选项(h|f|d|m|u|q): " choice
case $choice in
h)
menu
::
f)
echo "******磁盘分区信息"
blkid | cut -d: -f1
;;
q)
echo "程序正在退出,欢迎下次使用......."
exit 0
;;
m)
echo "******内存信息"
free -m
;;
*)
echo "请通过h查看帮助信息"
;;
esac
done
5.判断用户输入的字符串,如果是"hello",则显示"world";如果是"world",则显示"hello",否则提示"请输入hello或者world,谢谢!"
read -p "keyin: " name
case $name in
hello)
echo world
;;
world)
echo hello
;;
*)
echo "keyin hello or world"
;;
esac
6.有颜色的字符串输出
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
END="\033[0m"
print_color_string(){
color=$1
string=$2
case $color in
red)
echo -e "$RED $string $END"
;;
green)
echo -e "$GREEN $string $END"
;;
yellow)
echo -e "$YELLOW $string $END"
;;
*)
echo $string
;;
esac
}
print_color_string 'red' 'hello world'
print_color_string 'green' 'hello world'
print_color_string 'yellow' 'hello world'
7.用户建立脚本
• 执行users_create.sh userlist passlist
• 建立userlist列表中的用户
• 设定userlist列表中的密码为passlist列表中的密码
• 当脚本后面跟的文件个数不足两时,报错
• 当文件行数不一致时报错
• 当文件不存在时报错
• 当用户存在时报错
#1.脚本名称:users_create.sh
#2.脚本执行的格式:user_create.sh userlist passlist
#3.目标:创建用户(一般情况下只有root用户可以创建用户)
# [ `id -u` -eq 0 ] || echo red "Error:THis scipt must run as root!"
# [ `whoami` = `root` ] || echo ...
# [ $USER = `root` ] || echo ...
[ 'id -u ' -eq 0 ] || echo "Error:This script must run as root!"
#4.用户名文件和密码文件信息是一一对应的。
# 1)输入的参数是不是两个参数 [ $# -eq 2 ] || echo red "Error:please input users file and password file"
# 2)判断用户文件和密码文件是否存在
# [ -e $1 -a -f $1 ]
# [ -e $2 -a -f $2 ]
# 3)用户的行数和密码文件的行数是否一致
# users_line=`wc -l $1 | cut -d ' ' -f1`
# pwds_line=`wc -l $2 | cut -d ' ' -f1`
# [ $users_line != $pwds_line ] && echo red "Error:users file lines is differ password file"
[ $# -eq 2 ] || {
echo "Error:please input users file and password file"
exit 1
}
[ -e $1 -a -f $1 ] || {
echo "Error:user file not exists"
exit 1
}
[ -e $2 -a -f $2 ] || {
echo "Error:password file not exists"
exit 1
}
users_line=`wc -l $1 | cut -d ' ' -f1`
pwds_line=`wc -l $2 | cut -d '' -f1`
[ $users_line != $pwds_line ] && {
echo "Error:users file lines is differ password file"
exit 1
}
#4.所有可能出现问题的分析结束,创建用户
#max_line=`wc -l $1 | cut -d ' ' -f1`
#循环的序列:for num in `seq 1 $max_line`
#循环的过程:
# username = `sed -n $(num)p $1`
# password = `sed -n $(num)p $2`
# useradd $username(用户有可能存在)
# 如果用户创建成功:echo $password | passwd --stdin $username
max_line=`wc -l $1 | cut -d ' ' -f1`
for num in `seq 1 $max_line`
do
echo '.................................'
username=`sed -n $(num)p $1`
password=`sed -n $(num)p $2`
useradd $username
if [ $? -eq 0 ];then
echo "$username Create success";
echo $password | passwd --stdin $username
echo "$username set password success"
else
echo "$username Create failed"
fi
done
8.expect自动应答
#!/usr/bin/expect
#expect获取脚本后面传递的参数和bash环境不一样
#0,1,2
set ip [ lindex $argv 1 ]
spawn ssh kiosk@$ip
expect {
"(yes/no?)" { send "yes\n";}
"password:" { send "$password\n"}
}
#持久连接远程服务器
interact