用于在文件的每一行上执行命令的Shell脚本,其中包含空格分隔的字段

我正在尝试编写一个 shell 脚本,该脚本逐行读取文件并执行命令,其参数取自每行的空格分隔字段。

更准确地说,我需要使用 wget 从第二列中给出的 URL 下载一个文件到第一列中给出的路径。但我不知道如何加载此文件并获取脚本中的值。

文件.txt

file-18.log https://example.com/temp/file-1.log
file-19.log https://example.com/temp/file-2.log
file-20.log https://example.com/temp/file-3.log
file-21.log https://example.com/temp/file-4.log
file-22.log https://example.com/temp/file-5.log
file-23.pdf https://example.com/temp/file-6.pdf

期望的输出是

wget url[1] -o url[0]

wget https://example.com/temp/file-1.log -o file-18.log
wget https://example.com/temp/file-2.log -o file-19.log
...
...
wget https://example.com/temp/file-6.pdf -o file-23.pdf

回答

在 bash 中使用 read 和 while 循环逐行迭代文件并在每次迭代时调用 wget :

while read -r NAME URL; do wget "$URL" -o "$NAME"; done < File.txt


以上是用于在文件的每一行上执行命令的Shell脚本,其中包含空格分隔的字段的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>