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

这里的技术是共享的

You are here

自己亲自写的 在 /etc/hosts 文件中 增加一行解析 (记录)的代码 有大用 有大大用

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




grep -Fisv $domain

-F  表示--fixed-string 固定字符串,grep查找的内容不会当作正则表达式来搜索

-i  表示 忽略大小写

-s  表示静默模式 silent mode ,其作用是在搜索过程中不显示不存在或无匹配文本的错误信息。

-v 表示 反转


普通分类: