1、有这样一种情况,刚用vmware装的centos虚拟机,需要系统初始化,比如关闭防火墙和selinux
设置静态ip,下载常用的软件,比如lrzsz,net-tools,vim等等。
2、根据以上情况,为此写了一个脚本。
#!/bin/bash
# 检查当前系统是否为CentOS
is_centos() {
# 检查/etc/os-release文件中的ID字段
local os_id=$(grep -Ei '^ID=' /etc/os-release | cut -d'=' -f2 | tr -d '"')
if [[ -z "$os_id" ]]; then
# 如果/etc/os-release不存在或没有ID字段,尝试检查/etc/redhat-release
if [[ -f /etc/redhat-release ]]; then
# CentOS 6或更早版本可能没有/etc/os-release,但会有/etc/redhat-release
local redhat_release=$(cat /etc/redhat-release)
if [[ $redhat_release == CentOS* ]]; then
return 0
fi
fi
return 1 # 既不是CentOS也不是其他Red Hat系的发行版
fi
# 检查ID字段是否为centos
if [[ "$os_id" == 'centos' ]]; then
return 0 # 是CentOS系统
else
return 1 # 不是CentOS系统
fi
}
# 调用函数并检查返回值
if is_centos; then
echo -e "\033[32m当前系统为CentOS,继续执行脚本...\033[0m"
echo "请选择要执行的操作:"
echo "1. 配置本地网卡固定ip和子网掩码,DNS"
echo "2. 系统初始化"
echo "3. xtrabackup物理备份数据库并添加定时任务计划"