遍历输入文件对 [英] looping through pairs of input files

查看:49
本文介绍了遍历输入文件对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bash脚本来批量执行现有程序包.每次执行的输入需要两个输入文件.我可以使用常规名称,也可以将每对放置在其自己的子目录中,以确保每对保持在一起.我有些零散,但是没有什么可以循环两个文件或进入一个子目录,执行某些操作,向上移动并在下一个子目录中重复.

I'm trying to us bash scripting to batch execute an existing package. The input for each execution requires two input files. I can either use regular names or place each pair in its own sub-directory to make sure each pair stays together. I've got bits and pieces but nothing that loops over TWO files or enters a sub-directory, does something, moves up and repeats on the next sub-directory.

这将在包含文件对的单个目录上执行我想要的操作(每个文件对被命名为例如 ABC10f.txt ABC10r.txt ):

This will perform what I want on a single directory containing the pair of files (each file pair is named e.g. ABC10f.txt and ABC10r.txt):

#!/bin/bash
#
f=$(ls *f.txt)
  echo "forward -> $f"
i=$(ls *r.txt)
  echo "reverse -> $i";
/path/to/package [options] $f $i;

以下内容将循环遍历主目录中的每个子目录,但仅在到达最后一个目录时才执行我的操作(注意:我正在使用 helloworld.sh 对其进行测试–我认为如果我可以列出每个子目录并在执行每个操作后回显"hello world",那么我会得到所有子目录的列表,后跟一个"hello world"):

The following will loop through each sub-directory in my main directory but only executes what I only when it gets to the last one (Note: I was testing it with helloworld.sh – I thought if I could get it to list each sub-dir and echo "hello world" after each I'd be on my way to doing what I want; instead I get a list of all sub-directories followed by a single "hello world"):

#!/bin/bash
#
myCommand=/path/to/scripts/helloworld.sh

for d in ./; do
  find * -maxdepth 1 -type d 
    cd $d
    echo $PWD
    exec "$myCommand"
done

将其组合在一起有什么帮助吗?

A little help putting this together?

推荐答案

使您的 helloworld.sh 看起来像:

#!/bin/bash

for f in *f.txt; do
    r="${f/f.txt/r.txt}"
    echo "  f: $f"
    echo "  r: $r"
    /path/to/package [options] "$f" "$r";
done

第二点:

#!/bin/bash
# '*/' pattern matches all of the subdirectories in the current directory
for d in */; do
    # doing any actions in a subshell ()
    (cd "$d" && echo "$(pwd)" && bash /path/to/scripts/helloworld.sh)
done

这篇关于遍历输入文件对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆