#!/bin/bash
设置服务状态
function service() { # 读取配置文件 cat ‘service.txt’ | while read -r line do
# 跳过空行
if [[ ! $line ]]; then
continue
fi
# 分离 '='
unit=$(echo $line | awk -F ' = ' '{print $1}')
active=$(echo $line | awk -F ' = ' '{print $2}')
# 执行配置命令
if [ $active = 'active' ]; then
if systemctl start $unit && systemctl enable $unit; then
echo "[ ok ] set $unit to $active"
else
echo "[ failed ] set $unit to $active"
fi
else
if systemctl stop $unit && systemctl disable $unit; then
echo "[ ok ] set $unit to $active"
else
echo "[ failed ] set $unit to $active"
fi
fi
done }
设置内核参数
function kernel_param() { # 读取配置文件 cat ‘kernel_param.txt’ | while read -r line do
if [[ ! $line ]]; then
continue
fi
key=$(echo $line | awk -F ' = ' '{print $1}')
value=$(echo $line | awk -F ' = ' '{print $2}')
if grep -q "$key=" /etc/sysctl.conf ;then
sed -i "s/$key=.*/$key=$value/g" /etc/sysctl.conf
if sysctl -wq "$key=$value";then
echo "[ ok ] set $key to $value"
else
echo "[ failed ] set $key to $value"
fi
else
echo "$key=$value" >> /etc/sysctl.conf
if sysctl -w "$key=$value"; then
echo "[ ok ] set $key to $value"
else
echo "[ failed ] set $key to $value"
fi
fi
done # 加载内核参数配置文件 sysctl -p }
设置主机名
function hostname() { name=\((eval cat 'hostname.txt') if hostnamectl set-hostname \)name; then
echo "[ ok ] set hostname to $name"
else
echo "[ failed ] set hostname to $name"
fi }
循环执行每个传入的类别
for class in \(@ do eval \)class done