欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

update_hosts 自己亲自做的 更新 添加 三条记录到 hosts bash 脚本 有大用 有大大用

#!/bin/bash
#
# 定义要添加的hosts条目
new_entries=(
    "192.168.0.1 aaaa1.com"
    "192.168.0.2 aaaa2.com"
    "192.168.0.3 aaaa3.com"
)

# 备份原始hosts文件
backup_path="/etc/hosts.bak"
sudo \cp -a /etc/hosts backup_path
echo "已创建hosts备份: $backup_path"

# 检查并添加新条目
added=false
for entry in "${new_entries[@]}"; do
    # 检查条目是否已存在(忽略注释和空格)
    if ! grep -qF "$entry" /etc/hosts; then
        echo "$entry" | sudo tee -a /etc/hosts > /dev/null
        echo "已添加: $entry"
        added=true
    else
        echo "已存在: $entry"
    fi
done

# 显示通知弹窗
if [ "$added" = true ]; then
(/usr/bin/osascript -e  'display dialog "hosts文件更新成功!" buttons {"OK"}   default button "OK"')
else
(/usr/bin/osascript -e  'display dialog "未添加新条目(条目已存在)" buttons {"OK"}   default button "OK"')
fi


普通分类: