【问题标题】:How to pipe files one by one from list into script?如何将列表中的文件一一通过管道传输到脚本中?
【发布时间】:2015-12-26 10:05:00
【问题描述】:

我有一个需要通过管道传输到 shell 脚本的文件列表。我可以使用以下命令列出目录中的文件:

ls ~/data/2121/*SOMEFILE*

导致:

2121.SOMEFILEaa
2121.SOMEFILEab
2121.SOMEFILEac
and so on...

我有另一个脚本,它对我使用以下命令运行的单个文件 (2121.SOMEFILEaa) 执行一些处理:

bash runscript ../data/2121/2121.SOMEFILEaa

但是,我需要通过将ls 生成的文件列表中的单个文件传送到脚本中来提高效率。如何将来自ls ~/data/2121/*SOMEFILES* 命令的结果——逐个文件——传送到runscript 脚本中?

【问题讨论】:

  • 您真的想将所有文件通过管道传输到一个脚本实例中,还是多次调用该脚本,每个文件一次?

标签: linux bash for-loop ls


【解决方案1】:

另一种选择

ls ~/data/2121/*SOMEFILE* | xargs -L1 bash runscript

【讨论】:

    【解决方案2】:

    我想你正在寻找这个:

    for file in ~/data/2121/*SOMEFILE*; do
        bash runscript "$file"
    done
    

    这样,您为每个文件调用bash runscript

    【讨论】:

      【解决方案3】:
      $ cat pipe.sh
      
      #!/bin/bash
      ## Store data from pipe to variable $PIPE ------#
      _read_pipe(){                                   #
          while read -t 10 pipe; do
              if [ -n "$pipe" ] ;then
                  PIPE="$PIPE $pipe" ;fi ;done ;}
      
      
      ## your code -----------------------------------#
      _read_pipe                                      #   
      for kung_foo in $PIPE ;do
          echo $kung_foo ;done
      

      $ ls 2121.SOMEFILE* | ./pipe.sh
      2121.SOMEFILEaa
      2121.SOMEFILEab
      2121.SOMEFILEac
      and so on...
      

      [ -t ] 用于超时

      我希望这会有所帮助, 干杯卡里姆

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 1970-01-01
        相关资源
        最近更新 更多