【问题标题】:How can I combine two separate scripts being piped together to make one script instead of two?如何将两个单独的脚本组合在一起以制作一个脚本而不是两个?
【发布时间】:2016-06-19 22:14:08
【问题描述】:

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

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

而script2以:

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

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

编辑 - 问题的答案有点有趣。生活和学习。

【问题讨论】:

    标签: bash shell ffmpeg youtube-dl


    【解决方案1】:

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

    #!/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"
    

    【讨论】:

    • @RanyAlbegWein 你的意思是 youtubeResult 分配,不是吗?我已经更新了答案。
    • 赋值运算符前后有空格。
    • @RanyAlbegWein 谢谢,我没有注意到这一点。
    • @tripleee 感谢您提供这个有用的网站。
    【解决方案2】:

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

    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    
    

    【讨论】:

      猜你喜欢
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 2014-10-02
      • 2015-10-19
      • 2016-09-03
      • 1970-01-01
      相关资源
      最近更新 更多