#!/bin/sh
#
line=$(/usr/bin/osascript -e 'display dialog "请输入一行 host 记录 例如 " & return & "192.168.0.100 www.abc.com" default answer "" with icon note buttons { "Continue"} default button "Continue" set result1 to result
set the text_data to text returned of the result
set the button_data to button returned of the result1
if button_data = "Cancel" then
return ""
end if
return text_data')
if [ -z "$line" ];then
(/usr/bin/osascript -e 'display dialog "没有添加host记录"')
exit 0;
fi
domain=`echo $line | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | awk '{print $NF}'`
#remove have exists
if [ -n "$domain" ];then
str="$(cat /etc/hosts | grep -Fisv $domain)"
echo "$str" > /etc/hosts
fi
echo $line >> /etc/hosts