当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 7 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 我想修改代理IP,怎么才能让它在.sh文件里生效
帖子发表于 : 2016-07-02 18:11 

注册: 2010-02-12 19:47
帖子: 68
送出感谢: 19
接收感谢: 0 次
#!/bin/bash
echo "Do you want to change the IP?"
read answer
if
[ $answer = n ] || [ $answer = N ]
then
echo "Your file name:"
read c
echo "Your address:"
read d
wget -O $c $d
else
a="http\_proxy\=http\:\/\/"
echo "Print the IP:"
read b
sed -i "s/^$a.*/$a$b/g" `grep ^$a.* -rl /home/y/.bashrc`
source /home/y/.bashrc #修改完IP在终端里这么写可以生效,想把它写在.shell文件里怎么办
echo "Your file name:"
read c
echo "Your address:"
read d
wget -O $c $d
fi


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 我想修改代理IP,怎么才能让它在.sh文件里生效
帖子发表于 : 2016-07-02 18:33 
头像

注册: 2011-03-20 13:12
帖子: 4521
地址: 山东省寿光县
系统: Debian
送出感谢: 105
接收感谢: 997
sluding 写道:
a="http\_proxy\=http\:\/\/"
read b
sed -i "s/^$a.*/$a$b/g" `grep ^$a.* -rl /home/y/.bashrc`
source /home/y/.bashrc #修改完IP在终端里这么写可以生效,想把它写在.shell文件里怎么办

没理解错的话 a 的前面应该加上 export?
参考 http://ss64.com/bash/export.html

另参考 https://wiki.archlinux.org/index.php/proxy_settings#Environment_variables
中 "Alternatively you can automate the toggling of the variables by adding a function to your .bashrc " 一段
方便切换代理


_________________
Debian 中文论坛 - forums.debiancn.org
欢迎所有 Debian GNU/Linux 用户


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 我想修改代理IP,怎么才能让它在.sh文件里生效
帖子发表于 : 2016-07-02 22:21 

注册: 2010-02-12 19:47
帖子: 68
送出感谢: 19
接收感谢: 0 次
vickycq 写道:
sluding 写道:
a="http\_proxy\=http\:\/\/"
read b
sed -i "s/^$a.*/$a$b/g" `grep ^$a.* -rl /home/y/.bashrc`
source /home/y/.bashrc #修改完IP在终端里这么写可以生效,想把它写在.shell文件里怎么办

没理解错的话 a 的前面应该加上 export?
参考 http://ss64.com/bash/export.html

另参考 https://wiki.archlinux.org/index.php/proxy_settings#Environment_variables
中 "Alternatively you can automate the toggling of the variables by adding a function to your .bashrc " 一段
方便切换代理


谢谢~
没看太懂。
1、 您是想说 export http_proxy 么?后来试了下,没什么用啊
export 不是用来传递变量的么?网上有个帖子是这么改.bashrc的:
http_proxy=http://1.1.1.1:80
export http_proxy # 变量不都是以 $ 开头的么?为什么这里不用啊
2、网上看了个帖子说 以 #!/bin/bash 开头的就算是开了个新shell,是个新的子shell,export 或 source可以把变量传递给父shell么?
3、后来改了个格式,不知道这样是不是看地更清楚点

#!/bin/bash
main(){
echo "Your file name:"
read c
echo "Your address:"
read d
wget -O $c $d
}
echo "Do you want to change the IP?"
read answer
if
[ $answer = n ] || [ $answer = N ]
then
main
else
a="http\_proxy\=http\:\/\/"
echo "Print the IP:"
read b
sed -i "s/^$a.*/$a$b/g" `grep ^$a.* -rl /home/y/.bashrc`
export http_proxy
source /home/y/.bashrc #还是没什么用

main
fi


页首
 用户资料  
 
4 楼 
 文章标题 : Re: 我想修改代理IP,怎么才能让它在.sh文件里生效
帖子发表于 : 2016-07-03 0:02 
头像

注册: 2011-03-20 13:12
帖子: 4521
地址: 山东省寿光县
系统: Debian
送出感谢: 105
接收感谢: 997
sluding 写道:
# 变量不都是以 $ 开头的么?为什么这里不用啊

$ 是展开变量内容用的

sluding 写道:
以 #!/bin/bash 开头的就算是开了个新shell

只是规定了其使用的解释器

sluding 写道:
source /home/y/.bashrc #还是没什么用[/color]

建议直接说明最终需求,看有无其它方法

source /home/y/.bashrc 是在脚本所在的子shell中跑的,对其父shell即当前shell无效

可想办法绕过此问题
假设此脚本路径为 /home/y/bin/setproxy
将脚本中的 source /home/y/.bashrc 移除
在 /home/y/.bashrc 中设置 alias xxx='/home/y/bin/setproxy; source /home/y/.bashrc'
然后执行 xxx 即可

建议更换思路 参考楼上的 arch wiki 链接
通过在 /home/y/.bashrc 中定义函数实现

参考
http://stackoverflow.com/questions/11457880/source-bashrc-from-script-failing
http://stackoverflow.com/questions/23118624/source-bashrc-from-bash-script
http://askubuntu.com/questions/64387/can-not-successfully-source-bashrc-from-a-shell-script

或直接说明最终需求,看有无其它方法


_________________
Debian 中文论坛 - forums.debiancn.org
欢迎所有 Debian GNU/Linux 用户


页首
 用户资料  
 
5 楼 
 文章标题 : Re: 我想修改代理IP,怎么才能让它在.sh文件里生效
帖子发表于 : 2016-07-04 8:57 

注册: 2010-02-12 19:47
帖子: 68
送出感谢: 19
接收感谢: 0 次
vickycq 写道:
或直接说明最终需求,看有无其它方法


我想写个shell,在终端里输入代理IP即写入.bashrc,然后用 wget 下载链接


页首
 用户资料  
 
6 楼 
 文章标题 : Re: 我想修改代理IP,怎么才能让它在.sh文件里生效
帖子发表于 : 2016-07-04 15:52 
头像

注册: 2011-03-20 13:12
帖子: 4521
地址: 山东省寿光县
系统: Debian
送出感谢: 105
接收感谢: 997
sluding 写道:
我想写个shell,在终端里输入代理IP即写入.bashrc,然后用 wget 下载链接

如果坚持想用楼上的脚本,在脚本中 export 一次 http_proxy 即可
read b 输入代理地址 http://192.168.1.106:8087
然后 export http_proxy=$b
这样对脚本所处的子shell有效

sluding 写道:
source /home/y/.bashrc #还是没什么用

可能原因 /home/y/.bashrc 中没有 export
即 应该这样:
/home/y/.bashrc 中包含这样的语句:
export http_proxy="http://192.168.1.106:8087"
然后在脚本中对这一行替换操作

但根据楼上已经做出的说明,此脚本退出后返回到父 shell 中代理仍然不会有变化


_________________
Debian 中文论坛 - forums.debiancn.org
欢迎所有 Debian GNU/Linux 用户



_________________
评价: 3.7% sluding
 
页首
 用户资料  
 
7 楼 
 文章标题 : Re: 我想修改代理IP,怎么才能让它在.sh文件里生效
帖子发表于 : 2016-07-05 11:40 

注册: 2010-02-12 19:47
帖子: 68
送出感谢: 19
接收感谢: 0 次
[quote="vickycq"][/quote]

修改 $HOME/.wgetrc 来地利索点,不用 source 或 重新打开终端,不用 .bashrc 了

谢谢~


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 7 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译