Skip to content

Instantly share code, notes, and snippets.

@panzhongxian
Last active July 18, 2023 12:26
Show Gist options
  • Save panzhongxian/ca614fd48c35e4ab6ab63242083c334b to your computer and use it in GitHub Desktop.
Save panzhongxian/ca614fd48c35e4ab6ab63242083c334b to your computer and use it in GitHub Desktop.
keep focus

This script will redirect the websites which may affect your concentration to /etc/hosts file.

websites=(
"mp.weixin.qq.com" # 微信公众号/订阅号
"km.woa.com" # 乐问
"github.com" # 全球最大同性交友网站
"weibo.com" # 我是不刷微博的
# <= 瘾大的自己在后边继续添加
)
anchor1="## focus host setting: refusing the following website accessing"
anchor2="## focus host setting end"
hosts_file=/etc/hosts
function focus_on() {
s=$anchor1"\n"
for website in ${websites[@]}; do
s+="127.0.0.1 ${website}\n"
done
s+=$anchor2
grep "$anchor1" $hosts_file &> /dev/null
if [[ $? == 1 ]]; then
echo $s >> $hosts_file
fi
}
function focus_off() {
grep "$anchor1" $hosts_file &> /dev/null
if [[ $? == 1 ]]; then
return
fi
l1=`grep -n "$anchor1" $hosts_file | awk -F':' '{print $1}'`
l2=`grep -n "$anchor2" $hosts_file | awk -F':' '{print $1}'`
sed -i.focus_bak "${l1},${l2}d" $hosts_file
}
if [[ $1 == "focus_off" ]]; then
focus_off
else
focus_on
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment