Linux 如何通过远程文件读取 BASH 中的参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8978261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i cat a remote file to read the parameters in BASH?
提问by
How can i cat a remote file? Where it works for local file only.
我如何才能获取远程文件?它仅适用于本地文件。
#!/bin/bash
regex='url=(.*)'
# for i in $(cat /var/tmp/localfileworks.txt);
for i in $(cat http://localhost/1/downloads.txt);
do
echo $i;
# if [[ $i =~ $regex ]]; then
#echo ${BASH_REMATCH[1]}
#fi
done
cat: http://localhost/1/downloads.txt: No such file or directory
cat: http://localhost/1/downloads.txt: 没有那个文件或目录
采纳答案by ruakh
Instead of cat
, which reads a file from the file-system, use wget -O- -q
, which reads a document over HTTP and writes it to standard output:
而不是cat
,它从文件系统读取文件,而是使用wget -O- -q
,它通过HTTP读取文档并将其写入标准输出:
for i in $(wget -O- -q http://localhost/1/downloads.txt)
(The -O...
option means "write to the specified file", where -
is standard output; the -q
option means "quiet", and disables lots of logging that would otherwise go to standard error.)
(该-O...
选项表示“写入指定文件”,其中-
是标准输出;该-q
选项表示“安静”,并禁用大量日志记录,否则会导致标准错误。)
回答by Rob Wouters
You can use curl
:
您可以使用curl
:
curl http://localhost/1/downloads.txt
回答by halm
Why are you using a URL to copy from the local machine? Can't you just cat directly from the file?
为什么要使用 URL 从本地机器复制?你不能直接从文件中 cat 吗?
If you aredoing this from a remote machine and not localhost, then as far as I know you can't pass a URL to cat.
如果您是从远程机器而不是本地主机执行此操作,那么据我所知,您无法将 URL 传递给 cat。
I would try something like this:
我会尝试这样的事情:
scp username@hostname:/filepath/downloads.txt /dev/stdout
As someone else mentioned you could also use wgetinstead of scp.
正如其他人提到的,您也可以使用wget而不是scp。