ZBLOG

给下面的脚本 添加中文注释#!/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 ...

#!/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

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=251

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?