0%

shell学习笔记-II

流程控制:if/case

单分支结构

1
2
3
if 条件测试
then 命令序列
fi

双分支结构

1
2
3
4
if 条件测试
then 命令序列
else 命令序列
fi

多分支结构

1
2
3
4
5
6
7
8
if 条件测试1
then 命令序列

[elif 条件测试2
then 命令序列

elif 条件测试3
then 命令序列]...

case语法结构

1
2
3
4
5
6
7
8
9
10
11
12
13
case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)
无匹配后命令序列
esac

脚本实例

给用户一个提示模板

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash

red_col="\e[1;31m"
reset_col="\e[0m"

read -p "请确认安装kvm[y]: " kvm_install

if [ ! ${kvm_install} = "y" ];then
echo -e "$red_col 输入不正确! $reset_col"
exit
fi

判断一个用户是否存在

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
read -p "please input username: " username
id $username &>/dev/null
if [ $? -eq 0 ];then
echo "$username already exists"
else
useradd $username
if [ $? -eq 0 ];then
echo "$username is created."
fi
fi

判断当前系统版本,下载对应yum配置文件

if…else方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
os_version=`cat /etc/redhat-release |awk '{print $4}'|awk -F "." '{print $1}'`
repo_bk=/etc/yum.repos.d/bk/
[ -d $repo_bk ] || mkdir -p $repo_bk
mv /etc/yum.repos.d/*.repo $repo_bk

if [ "$os_version" = "7" ];then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
echo "CentOS$os_version yum configure is already."
elif [ "$os_version" = "6" ];then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo &> /dev/null
echo "CentOS$os_version yum configure is already."
elif [ "$os_version" = "8" ];then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &> /dev/null
echo "CentOS$os_version yum configure is already."
else
echo "The system version is not supported."
fi

case 方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
os_version=`cat /etc/redhat-release |awk '{print $4}'|awk -F "." '{print $1}'`
repo_bk=/etc/yum.repos.d/bk/
[ -d $repo_bk ] || mkdir -p $repo_bk
mv /etc/yum.repos.d/*.repo $repo_bk

case $os_version in
7)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
echo "CentOS$os_version yum configure is already."
;;
6)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo &> /dev/null
echo "CentOS$os_version yum configure is already."
;;
8)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &> /dev/null
echo "CentOS$os_version yum configure is already."
;;
*)
echo "The system version is not supported."
esac

yum安装apache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
#install apache
#v1.0 by arlo 2022/7/20
gateway=192.168.6.254

ping -c1 t.cn &>/dev/null
if [ $? -eq 0 ];then
yum -y install httpd
systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-port=80/tcp --zone=public
firewall-cmd --reload
sed -ri s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
setenforce 0
curl http://127.0.0.1 &>/dev/null
if [ $? -eq 0 ];then
echo "Apache ok..."
fi
elif ping -c1 $gateway &>/dev/null;then
echo "check dns..."
else
echo "check ip address"
fi