流程控制if 字符比较
1. [ "$a" == "$b"]
2. != 不等于
3. -z 字符串长度为零为真
4. -n 字符串长度不为空则为真 [ -n "$a" ]
5. str1>str2
# 多条件对比
a : and
o : or
[ 1 -lt 2 -a 5 -lt 10];echo $?
# 正则对比会使用 [[]]
&& 且
|| 或
[[ 1 -lt 5 || 5 -gt 10 ]];echo $?
例: 判断学生优劣
1-59 补考
60-80 及格
80-100 优秀
read -p "输入分数" grade
if [ $grade -lt 60 ];then
pass
elif [ $grade -ge 80 ];then
pass
elif [ $grade -ge 60 ];then
pass
else
pass
if [ $grade -gt 0 a $grade -lt 60];then
pass
# 正则匹配
[[ "$USER" =~ ^r ]]
判断变量是不是全都是数字
[[ "$num"=~ ^[0-9]+$ ]]
例: 批量创建用户, 输入创建用户的前缀以及后缀 aaa 123
read -p "前缀:" qz
read -p "后缀" hz
if [[ ! $qz=~^[a-z]+$ ]];then
if [[ #hz =~ ^[0-9]+$ ]];then
user = ${qz}${hz}
id $user &>/dev/null
if [ $? -eq 0];then
echo "已存在"
例 : root 用户清空/var/log/message 日志,并每次执行保留最近100行
# 切换用户 su - octivia123
# $0 可以取到脚本的名字
#!/usr/bin/bash
# 判断root
if [ $USER == "root" ] && [ $UID -eq 0 ] ;then
# 文件必须存在
if [ -f /var/log/messages ];then
# 保留最近100行
tail -100 /var/log/messages > /var/log/messages/bak
cat /var/log/messages.bak > /var/log/messages
else
echo "file do not exist"
else
echo "do not have the right to execute file $0 "
fi
判断httpd服务是否正常启动,文件名必须是httpd_daemon.sh
# 手动判断服务是否正常
ps aux netstat -lntp lsof systemctl telnet namp
# ps aux|grep httpd|grep -v grep
#!/usr/bin/bash
# 判断服务是否正常启动
ssh_status=$(systemctl status sshd|awk '/^.*Active/ {print $2}')
ssh_status=$(systemctl status sshd|awk '/^.*Active/ {print $2}')
if [ "$ssh_status" == "active" ];then
echo "is active"
fi
# 判断端口是否存在
netstat -lntp|grep "sshd" &>/dev/null
if [ $? -eq 0 ];then
sleep 1
echo "is alive"
else
echo "is dead"
fi
# 判断进程是否存在
ps aux|grep sshd|grep -v|grep -v "pts"
if [ $? -eq 0 ];then
echo "exist"
else
echo "do not exist"
fi
根据不同的系统装yum 源
# cat /etc/redhat-release 可以查看系统版本
tt=$(awk '{print $(NF-1)}' /etc/redhat-release)
echo ${tt%%.*}
#!/usr/bin/bash
tt=$(awk '{print $(NF-1)}' /etc/redhat-release)
if [ ${tt%%.*} -eq "7" ];then
#配置仓库
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat >/etc/yum.repos.d/base.repo<<-EOF
[base]
name=Local Base Yum Source
baseurl=ftp://192.168.56.1/base/7/x86_64
enable=1
gpgcheck=0
EOF
echo "7配好了"
elif [ ${tt%%.*} -eq "6" ];then
# 配置仓库
echo " 6配好了"
fi
安装不同版本的PHP
cat <<-EOF
1. Installed PHP 5.5
2. Installed PHP 6.6
3. Installed PHP 7.7
EOF
read -p " 请选择 [1|2|3|q]:" install
# 判断输入的必须是整数
if [[ ! $install =~ ^[1-4]$ ]];then
echo "输入错误"
exit
fi
if [ $install -eq 1 ];then
echo " php5.5"
elif [ $install -eq 2 ];then
echo "php6.6"
elif [ $install -eq 3 ];then
echo "php7.7"
elif [ $install == 'q' ];then
exit
fi
备份Mysql 中的所有数据库,每一个库一个.sql文件,排除没用的
# mysql -hlocalhost -uroot -p123 -e "show databases;"
排除一些库 _schema, test, mysql
|sed 1d|grep -v ".*_schema"
|sde 1d|grep -Ev ".*_schema|test|mysql"
拼接mysqldump 语句
|sed -r 's#(.*)#mysqldump -uroot -p -B \1 >1.sql#g'
# 方式二, 循环
#!/usr/bin/bash
dd=$(mysql -uroot -p -e "show databases;"|sed 1d|grep -Ev ".*_schema|test|mysql")
for i in $db
do
#用mysqldump
mysqldump -uroot -p -B $i > /backup/${i}_$(date +%F).sql
done
原文地址:https://www.cnblogs.com/Afrafre/p/11405715.html
时间: 2024-10-15 15:03:46