1. ed命令写hosts、删除指定的hosts配置
echo --------------------------修改hosts文件 ,在末尾追加一行, /etc/hosts--------------------------
(echo 'a';echo '127.0.0.1 https://www.baidu.com ';echo '.';echo 'wq')|ed /etc/hosts
echo --------------------------恢复hosts文件 ,删除指定行的配置, /etc/hosts--------------------------
(echo 'd';echo '127.0.0.1 https://www.baidu.com';echo '.';echo 'wq')|ed /etc/hosts
2. awk分割字符串,循环,写文件
#!/bin/sh
echo -------------------------- awk insert file --------------------------
x=‘10.35.45.84 mobile-api2011.baidu.com;127.0.0.1 elp.baidu.com;192.168.231.99 minder.baidu.com;’
#echo $x | awk '{ split($0,arr,";");for( i in arr) {print arr[i] >> "/etc/hosts"}}'