如何 cat 远程文件来读取 Bash 中的参数?

发布于 2024-12-28 16:22:07 字数 386 浏览 2 评论 0原文

如何cat远程文件?目前,它仅适用于本地文件。

#!/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: 没有这样的文件或目录

How can I cat a remote file? Currently, it works for local files 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬花如无物 2025-01-04 16:22:07

您可以使用curl

curl http://localhost/1/downloads.txt

You can use curl:

curl http://localhost/1/downloads.txt
秉烛思 2025-01-04 16:22:07

使用 wget -O- -q 代替从文件系统读取文件的 cat,它通过 HTTP 读取文档并将其写入标准输出

for i in $(wget -O- -q http://localhost/1/downloads.txt)

:( -O... 选项表示“写入指定文件”,其中 - 是标准输出;-q 选项表示“安静”; ",并禁用大量日志记录,否则会导致标准错误。)

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:

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.)

橙幽之幻 2025-01-04 16:22:07

为什么要使用 URL 从本地计算机复制?你不能直接从文件中猫吗?

如果您从远程计算机而不是本地主机执行此操作,那么据我所知,您无法将 URL 传递给 cat。

我会尝试这样的事情:

scp username@hostname:/filepath/downloads.txt /dev/stdout

正如其他人提到的,您也可以使用 wget 而不是 scp

Why are you using a URL to copy from the local machine? Can't you just cat directly from the file?

If you are doing this from a remote machine and not localhost, then as far as I know you can't pass a URL to cat.

I would try something like this:

scp username@hostname:/filepath/downloads.txt /dev/stdout

As someone else mentioned you could also use wget instead of scp.

游魂 2025-01-04 16:22:07

一个简单的解决方案:

content=$(curl -L https://example.com)
echo $content

如果您不想看到curl日志:

content=$(wget https://example.com -q -O -)
echo $content

An easy solution:

content=$(curl -L https://example.com)
echo $content

If you don't want to see the curl logs:

content=$(wget https://example.com -q -O -)
echo $content
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文