在linux中,我们可以通过export http_proxy=http://xxxx:x
来更改terminal中http_proxy的variables。 但我想用脚本来做。 所以我有以下代码:
#!/bin/bash FILE="_reliable_list.txt" for line in $(cat $FILE) do proxy="http://$line" export http_proxy=$proxy done
当我检查variableshttp_proxy的值时,在相同的terminal上,它是空的。 我想从文件代理地址读取,并将其导出到http_proxyvariables,并在使用wget后。 简单地说,我想自动化这个代码:
export http_proxy=http://xxxx:x wget mysite.com
如果有其他想法,我会很高兴。
您可以更改脚本以嵌入wget调用,假设包含url的新文件:
#!/bin/bash URLS="_url_list.txt" FILE="_reliable_list.txt" i=1 for line in $(cat $FILE) do export http_proxy="http://$line" wget $(head $i $URLS | tail -1 | tr -d '\n') let i++ done
首先,你想source
文件,而不是运行它。 当您使用export
,它不会传播到调用shell。
其次,你的代码不是很好。 你应该使用bash
的文件读取功能而不是cat
。 此外,您的代码只是选择该文件中的最后一行,并将其用作代理,你确定这就是你想要做的? 如果是这样,你可以使用tail
。
我总是使用“while read”来代替cat FILE。
我不知道这是否有所作为,或者是否有理由相互使用。
例:
while read LINE do export http_proxy="http://${LINE}" done < $INPUT_FILE
您可以简单地在命令行上执行此操作:
export http_proxy="$(tail -1 _reliable_list.txt)"
或者,如果你有一个脚本的上面的行说script.sh然后像这样运行:
. ./script.sh
在当前shell中获取导出的变量。