#!/bin/bash
定义 service 函数,用于设置服务
function service() { # 从 service.txt 文件中读取配置 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 }
定义 kernel_param 函数,用于设置内核参数
function kernel_param() { # 从 kernel_param.txt 文件中读取配置 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 }
定义 hostname 函数,用于设置主机名
function hostname() { # 从 hostname.txt 文件中读取主机名 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