1. The structure of the branch statement in the shell
if 条件测试1; then
命令序列
elif 条件测试2; then
命令序列
elif 条件测试3; then
命令序列
else 命令序列
fi
2. Use if to install apache
#!/usr/bin/bash
#install apache
#v1.0
gateway=192.168.122.1
ping -c1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
yum -y install httpd
systemctl start httpd
systemctl enable httpd
firewall-cmd --premanent --add-service=http
firewall-cmd --premanent --add-service=https
firewall-cmd --reload
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
setenforce 0
elif ping -c1 $gateway &>/dev/null
echo "check dns..."
else
echo "check ip address"
fi
3. Case syntax structure
case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
*)
;;
esac
Note that the last one is not required;;, it should be noted that *) is similar to default in high-level languages. It should be noted that case can only be used for string pattern matching, and cannot be compared with the size of numbers.
4. Use case to delete users
Use if to delete, version one:
#!/usr/bin/bash
#delete user
#v1.0
read -p "Please input a username: " user
id $user &>/dev/null
if [ $? -ne 0 ]; then
echo "no such user $user"
exit 1
fi
read -p "Are you sure[y/n]: " action
if [ "$action" = "y" -o "$action" = "Y" -o "$action" = "yes" -o "$action" = "YES"]; then
userdel -r $user
echo "$user is deleted"
else
echo "good!"
fi
Use if to delete, version two:
#!/usr/bin/bash
#delete user
#v1.0
read -p "Please input a username: " user
case "$user" in
y|Y|yes|YES)
userdel -r $user
echo "$user is deleted!"
;;
esac
5. In the shell script, true and: have the same effect.
6. Landing on the springboard
Supplementary knowledge: Mainly related to shell login, etc/profile; etc/bashrc (public); each user has his own profile and bashrc.
If you are logging in with a password, you can write a script and place it under the login-related folder, and that's it.
The method of using the secret key to log in has been introduced in the previous blog.