我怎样才能将两个单独的脚本组合在一起以制作一个脚本而不是两个?

How can I combine two separate scripts being piped together to make one script instead of two?

我有两个脚本通过管道连接在一起。 script1.sh | script2.sh 最初它们是同一部分,但我永远无法使其正常工作。 script1 的最后一部分调用 youtube-dl 读取批处理文件并将列表 url 输出到终端。请注意尾随 - 允许 youtube-dl 从标准输入读取。

 cat $HOME/file2.txt | youtube-dl --ignore-config -iga -

脚本 2 开头为:

while read -r input
do
ffmpeg [arg] [input] [arg2] [output]

我没有看到什么导致脚本在两半组合时挂起,但如果将一个通过管道传输到另一半则可以正常工作?

编辑 - 问题的答案有点有趣。边学边学。

我可能会使用这样的东西(逐行处理):

#!/usr/bin/bash
inputFile="$HOME/file2.txt"
while read -r line
do
    youtubeResult=$(youtube-dl --ignore-config -iga - "$line")
    ffmpeg [arg] "$youtubeResult" [arg2] [output]
done < "$inputFile"

简短的回答是需要 | 才能使脚本协同工作。在上面的问题中,我最初有一个这样结束的脚本:

cat $HOME/file2.txt | youtube-dl --ignore-config -iga - 
while read -r input
do
ffmpeg [arg] [input] [arg2] [output] 

但这不起作用。我们需要通过管道进入 while 循环:

cat "$HOME/file2.txt" | youtube-dl --ignore-config -iga - | while read -r input

但是我们通过这样做以更有效的方式得到相同的结果:

youtube-dl --ignore-config -iga "$HOME/file2.txt" | while read -r input

或者如果您愿意:

youtube-dl --ignore-config -iga "$HOME/file2.txt" | \
while read -r input