站主补充:
补充一:Shell if 单判断的格式
if [ <judgment condition> ];then
<step to perform>
fi
补充二:Shell if 双判断的格式
if [ <judgment condition> ];then
<step to perform>
else
<step to perform>
fi
补充三:Shell if 多判断的格式
if [ <judgment condition> ];then
<step to perform>
elif
<step to perform>
else
<step to perform>
fi
补充四:Shell if 多条件判断的格式
4.1 与条件的判断
if [ <judgment condition> -a <judgment condition> ];then
<step to perform>
fi
或者:
if [ <judgment condition> ] && [ <judgment condition> ];then
<step to perform>
fi
4.2 或条件的判断
if [ <judgment condition> -o <judgment condition> ];then
<step to perform>
fi
或者:
if [ <judgment condition> ] || [ <judgment condition> ];then
<step to perform>
fi
补充五:Shell if 对条件进行转换后再判断
将变量转换成大写以后再进行判断
if [ `echo $<variable> | tr 'a-z' 'A-Z'` = "<character string>" ];then
<step to perform>
fi
#!/bin/bash
#At the system level, start the database as a MySQL user
chown -R mysql /var/lib/mysql
sed -i '/^user=/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i '/^datadir/a user=mysql' /etc/my.cnf.d/mariadb-server.cnf
#Disable client local data reading at the system level
sed -i '/^local-infile=/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i '/^datadir/a local-infile=0' /etc/my.cnf.d/mariadb-server.cnf
#At the system level, remote login of database is prohibited
sed -i '/^bind-address=/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i '/^datadir/a bind-address=127.0.0.1' /etc/my.cnf.d/mariadb-server.cnf
#Restart database
systemctl restart mariadb ; systemctl restart mysql
case <variable> in
<variable value 1>)
<the command to execute when the variable is this value>;;
<variable value 2>)
<the command to execute when the variable is this value>;;
<variable value 3>)
<the command to execute when the variable is this value>;;
*)
<the command to execute when the variable value is other>;;
esca
内容二:case 语句的使用案例
#!/bin/bash
read -p "Which one do you like better, eternalcenter eternalcentre ec-x : " name
case $name in
eternalcenter)
echo "Do you realy like $name better? eternalcenter?" ;;
eternalcentre)
echo "Do you realy like $name better? eternalcentre?" ;;
ec-x)
echo "Do you realy like $name better? ec-x?" ;;
*)
echo "So you don't like them all ." ;;
esac