问题来源
我有一个bash
程序,想要在执行该程序的时候修.bashrc
,然后更新一些环境变量,并在结束(ctrl+c)
的时候再把程序恢复原样。操作如下:
echo
命令把想要增加的内容写入.bashrc
- 执行一些功能
- 用
trap
命令获取是否结束的操作 - 备份
.bashrc
为 bashrc_bak
- 用
sed '/搜索内容/d' bashrc_bak > .bashrc
例子, 下面的例子是我设置代理的脚本,执行该脚本的时候会在.bashrc
中增加http_proxy
和https_proxy
的设置,关掉脚本后,删除之前增加的内容,并把http_proxy
和https_proxy
取消设置。
cp ~/.bashrc ./bashrc_bak ## back bashrc file
echo "###proxy" >> ~/.bashrc
echo "export http_proxy=http://127.0.0.1:7890" >> ~/.bashrc
echo "export https_proxy=http://127.0.0.1:7890" >> ~/.bashrc
source ~/.bashrc
trap 'recover_bashrc' EXIT ## trap exit (ctrl+c) signal then run recover_bashrc function
recover_bashrc()
{
sed -i '/http_proxy=http/d' ~/.bashrc
sed -i '/https_proxy=http/d' ~/.bashrc
sed -i '/###proxy/d' ~/.bashrc
echo "unset http_proxy" >> ~/.bashrc
echo "unset https_proxy" >> ~/.bashrc
source ~/.bashrc
sed -i '/unset http_proxy/d' ~/.bashrc
sed -i '/unset https_proxy/d' ~/.bashrc
}
./clash-linux-amd64 -d ./
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)