(1) read 测试PING
#!/bin/bash
read -p "请输入需要检测的IP地址: " IP
ping $IP -W1 -c1 &> /dev/null
rc=$?
if [ $rc -eq 0 ] ;then
echo "$IP 能正常通信"
else
echo "$IP 无法通信"
fi
(2)read 修改主机名
#!/bin/bash
read -p "请输入你要修改的主机名: " HostName
echo "你将要修改的主机名称为: $HostName"
read -p "你确定真的要修改如下内容[Y|N]?" rc
if [ $rc=="Y" ] ; then
hostnamectl set-hostname $HostName
else
exit
fi
(3)在每月的第一天准备并压缩/etc目录的所有内容,存放到/root/bak目录,存放的形式2022_03_28_etc.tar.gz,脚本名字为filebak.
#!/bin/bash
DestPath=/root/bak
Date=$(date +%Y_%m_%d)
[ ! -d $DestPath ] && mkdir -p $DestPath
tar czf $DestPath/${Date}_etc.tar.gz /etc
crontab -e (新建定时任务)
crontab -l (查看定时任务)
00 23 1 * * bash /root/shell/filebak &>/dev/null
crontab -r (删除定时任务)
(4)查看内存/当前内存.如果使用率超过80%则警告
#!/bin/bash
free_use=$(free -m | awk '/Mem/ {print$3/$2*100}')
if [ ${free_use%.*} -gt 80 ] ;then
echo "你的内存超过了80%,当前为${free_use}%" | mail -s "!!!!"
else
echo "你的内存正常,当前为${free_use}%"
fi
(5) 替换
#!/bin/bash
string="bei jing huan ying ning ,huan ying ning lai wan"
echo $string
cat << EOF
1) 打印string长度
2) 删除字符串中的所有的ying
3) 替换第一个huan为HUAN
4) 替换全部huan为HUAN
EOF
read -p "请输入数字1|2|3|4 : " var
if [ $var -eq 1 ] ;then
# echo "当前string的长度为: ${#string}"
echo ${string} | wc -L
elif [ $var -eq 2 ] ;then
echo ${string//ying/}
elif [ $var -eq 3 ]; then
echo ${string/huan/HUAN}
elif [ $var -eq 4 ]; then
echo ${string//huan/HUAN}
fi
(6) 数据库备份
#!/bin/bash
DestPath=/backup/mysql
[ -d $DestPath ] || mkdir -p $DestPath
read -p "请输入你需要备份的数据库名称: " db
read -p "请输入数据库的密码 " M_PASS
mysqldump -p$M_PASS --single-transaction -R -B $db > $DestPath/${db}_$(date +%F ).sql
if [ $? -eq 0 ] ; then
echo "----------"
echo "$db 数据库备份成功"
echo "----------"
fi
(7) 检测服务是否执行
#!/bin/bash
if [ $# -ne 1 ] ;then
echo "请输入一个服务名称"
exit
fi
systemctl status "$1" &>/dev/null
rc=$?
if [ $rc -eq 0 ] ;then
echo "$1 服务正在运行"
elif [ $rc -eq 4 ]; then
echo "$1 没有这个服务"
else
echo "$1 服务没有运行"
fi
(7)磁盘使用率监测
#!/bin/bash
disk=$(df -h | grep '/$' | awk '{print $(NF-1)}')
if [ ${disk/\%/} -ge 80 ] ;then
echo "你的磁盘使用率过高..${disk}"
else
echo "你的磁盘使用率正常...${disk}"
fi
(8) 成绩查询
#!/bin/bash
read -p "请输入你的分数:" fs
expr ${fs%.*} + 0 &> /dev/null # 排除字母与非整数与回车
if [ $? -ne 0 ] ;then
echo "请输入整数"
exit
fi
#if [ -z $fs ];then
# echo "不要直接回车,不支持"
# exit
#fi
if [ ${fs%.*} -gt 0 -a ${fs%.*} -lt 60 ] ;then
echo "补考"
elif [ ${fs%.*} -ge 60 -a ${fs%.*} -lt 80 ] ; then
echo "合格"
elif [ ${fs%.*} -ge 80 -a ${fs%.*} -le 100 ] ;then
echo "优秀"
fi
(9) 创建用户脚本,需要输入创建他用户的前缀(英文)以及后缀(数字)
#!/bin/bash
read -p "请输入用户的前缀:" qz
if [[ ! $qz =~ ^[a-Z]+$ ]] ;then
echo "你输入的不是英文" && exit
fi
read -p "请输入创建用户的后缀:" hz
if [[ $hz =~ ^[0-9]+$ ]]; then
user=${qz}${hz}
id $user &> /dev/null
if [ $? -eq 0 ];then
echo "用户已经存在 $user"
else
useradd $user
echo "用户创建成功 $user"
fi
fi
(10) 判断root 用户清空/var/log/messages日志,并每次执行保留最近100行
vim /etc/profile
59
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
先清空,删除(因进程占用,不会被释放)
#!/bin/bash
if [ $UID -eq 0 ] && [ $USER == "root" ] ;then
if [ -f /var/log/messages ] ;then
tail -100 /var/log/messages > /var/log/messages.bak
cat /var/log/messages.bak > /var/log/messages
echo "-------成功---------"
else
echo "文件/var/log/messages 不存在"
fi
else
echo "$USER对$0 没有执行权限"
fi
(11) 判断vsftpd服务是否正常启动
#!/bin/bash
ftp_status=$(systemctl status vsftpd | awk '/Active/ {print$2}')
if [ "$ftp_status" == "active" ]; then
sleep 1
echo "vsftpd服务的检测是${ftp_status}"
else
sleep 1
echo "vsftpd服务检测是${ftp_status}"
fi
netstat -tunlp | grep vsftp &> /dev/null
if [ $? -eq 0 ];then
sleep 1
echo "vsftpd服务端口存活"
else
sleep 1
echo "vsftpd 服务端口不存活"
fi
ps aux | grep vsftpd | grep -v grep &> /dev/null
if [ $? -eq 0 ]; then
sleep 1
echo "vsftpd服务进程存在"
else
sleep 1
echo "vsftpd服务进程不存在"
fi
(12)根据不同系统配置不同的yum源
#!/bin/bash
tt=$(awk '{print $(NF-1)}' /etc/redhat-release)
#version=rpm -q centos-release | awk -F [-] '{print $3}
if [ ${tt%%.*} -eq "6" ] ; then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
echo "Centos$tt 系统已经配置好yum仓库"
elif [ ${tt%%.*} -eq "7" ] ;then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
echo "Centos$tt 系统已经配置好yum仓库"
fi
(13)安装不同版本的PHP
#!/bin/bash
cat << EOF
-----------------------
| 1. Installed PHP 5.5
| 2. Installed PHP 5.6
| 3. Installed PHP 7.8
| 4. Exit
-----------------------
EOF
read -p "请输入你要安装的PHP版本[1|2|3|4]: " install
if [[ ! "$install" =~ ^[1-4]$ ]] ;then
echo "请输入[1|2|3|4]...."
exit
fi
if [ $install -eq 1 ];then
sleep 2
echo "Installed php5.5 Done...."
elif [ $install -eq 2 ];then
sleep 2
echo "Installed php5.6 Done...."
elif [ $install -eq 3 ];then
sleep 2
echo "Installed php6.8 Done...."
elif [ $install -eq 4 ]; then
exit
fi
(14)备份所有的数据库,并生成sql文件
db=$(mysql -uroot -p123456 -e "show databases;" | egrep -v "Database|information_schema|mysql|performance_schema|sys")
for i in $db
do
mysqldump -uroot -p123456 -B $i > /backup/${i}_$(date +%F).sql
done
(15)rsync的启动和停止的脚本
#!/bin/bash
source /etc/init.d/functions
rc=$1
if [ $rc == "start" ]; then
if [ ! -f /var/run/rsync.pid ]; then
touch /var/run/rsync.pid
rsync --daemon
action "Rsync Starting..." /bin/true
else
action "Rsync Service Running..." /bin/false
fi
elif [ $rc == "stop" ] ;then
if [ ! -f /var/run/rsync.pid ]; then
action "Rsync Service Stoping..." /bin/false
else
rm -rf /var/run/rsync.pid
pkill rsync
action "Rsync Stoping..." /bin/true
fi
elif [ $rc == "status" ];then
if [ ! -f /var/run/rsync.pid ]; then
echo "Rsync Service Status InActive"
else
Rsync_Status=$(ps aux | grep rsync | grep -v grep | grep -v $0 | awk '{print $2}')
echo "Rsync Sservice Static Active ("$Rsync_Status")"
fi
else
echo "USAGE: $0 {start|stop}"
exit
fi
方法二
#!/bin/bash
source /etc/init.d/functions
rc=$1
case $rc in
start)
if [ ! -f /var/run/rsync.pid ]; then
touch /var/run/rsync.pid
rsync --daemon
action "Rsync Starting..." /bin/true
else
action "Rsync Service Running..." /bin/false
fi
;;
stop)
if [ ! -f /var/run/rsync.pid ]; then
action "Rsync Service Stoping..." /bin/false
else
rm -rf /var/run/rsync.pid
pkill rsync
action "Rsync Stoping..." /bin/true
fi
;;
status)
if [ ! -f /var/run/rsync.pid ]; then
echo "Rsync Service Status InActive"
else
#Rsync_Status=$(ps aux | grep rsync | grep -v grep | grep -v $0 | awk '{print $2}')
Rsync_Status=$(ps aux | grep rsync | grep -v grep | grep -v pts | awk '{print $2}')
echo "Rsync Sservice Static Active ("$Rsync_Status")"
fi
;;
*)
echo "USAGE: $0(start|stop|status)"
exit
esac
(16)