gpt4 book ai didi

bash - 如何将多个命令通过管道传递给 bash?

转载 作者:行者123 更新时间:2023-12-05 04:33:29 27 4
gpt4 key购买 nike

我想检查远程网站上的一些文件。

这里是bash命令生成计算文件md5的命令

[root]# head -n 3 zrcpathAll | awk '{print $3}' | xargs -I {} echo wget -q -O - -i {}e \| md5sum\;
wget -q -O - -i https://example.com/zrc/3d2f0e76e04444f4ec456ef9f11289ec.zrce | md5sum;
wget -q -O - -i https://example.com/zrc/e1bd7171263adb95fb6f732864ceb556.zrce | md5sum;
wget -q -O - -i https://example.com/zrc/5300b80d194f677226c4dc6e17ba3b85.zrce | md5sum;

然后我将输出的命令通过管道传输到 bash,但只执行了第一个命令。

[root]# head -n 3 zrcpathAll | awk '{print $3}' | xargs -I {} echo wget -q -O - -i {}e \| md5sum\; | bash -v
wget -q -O - -i https://example.com/zrc/3d2f0e76e04444f4ec456ef9f11289ec.zrce | md5sum;
3d2f0e76e04444f4ec456ef9f11289ec -
[root]#

最佳答案

请您尝试以下方法:

while read -r _ _ url _; do
wget -q -O - "$url"e | md5sum
done < <(head -n 3 zrcpathAll)

我们不应该把 -i 放在 "$url"的前面。

[关于-i选项的解释]

wget 的联机帮助页说:

-i file
--input-file=file
Read URLs from a local or external file. [snip]
If this function is used, no URLs need be present on the command line. [snip]
If the file is an external one, the document will be automatically treated as html if the Content-Type matches text/html.Furthermore, the file's location will be implicitly used as basehref if none was specified.

文件 将包含 url 行,例如:

https://example.com/zrc/3d2f0e76e04444f4ec456ef9f11289ec.zrce
https://example.com/zrc/e1bd7171263adb95fb6f732864ceb556.zrce
https://example.com/zrc/5300b80d194f677226c4dc6e17ba3b85.zrce

而如果我们使用 -i url 选项,则首先使用 wgeturl 下载为包含 url 行的文件如上。在我们的例子中,url 是下载自身的目标,不是 url 列表,wget 会导致错误:No URLs found in url

即使 wget 失败,为什么命令只输出一行,而不是md5sum 的结果是三行?这似乎是因为 head 命令立即刷新了剩余的管道子进程失败时的行。

关于bash - 如何将多个命令通过管道传递给 bash?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71403947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com