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