如何在bash中打开/操作多个文件?

时间:2014-05-05 12:15:40

标签: bash unix outputstream

我有一个bash脚本,它利用本地工具箱来执行操作 我的问题很简单

我有多个文件,这些文件的数量相同,但我想先解决所有不同的时间步骤,然后使用工具箱执行一些操作,但我不确定我是否在正确的轨道上。

=============================================

该文件如下

INPUTS

fname = a very large number or files with same name but numbering
e.g wnd20121.grb
    wnd20122.grb
      .......
    wnd2012100.grb

COMMANDS

> cdo -f nc copy fname ofile(s) 

(如果这是ofile(s)=输出文件我怎样才能存储它以供后续使用?从命令中获取ofile(输出文件)并使用它/将其保存为下一个输入,产生一个新的后续编号的输出集合(2)

>cdo merge ofile(s) ofile2

(然后自动获取ofile(s)2并将它们输入到下一个命令,依此类推,始终生成一组新的输出文件,其中包含我设置的特定集合名称,但区分它们的编号不同)

>cdo sellon ofile(s)2  ofile(s)3

------------------------------------

为了让我的问题更加清晰,我想知道我可以通过bash脚本基本上指示终端来获取"抓住"多个文件通常具有相同的名称但具有不同的编号,以使其记录的时间分开

e.g。 file1 file2 ... file n

然后获得多个输出,每个输出对应于它转换的文件的编号。

e.g。 output1 output2 ... outputn

如何设置这些参数,以便在生成它们的那一刻将它们存储起来供以后在命令中使用?

1 个答案:

答案 0 :(得分:1)

你的问题并不清楚,但也许以下内容会有所帮助;它演示了如何使用数组作为参数列表以及如何逐行将命令输出解析为数组:

#!/usr/bin/env bash

# Create the array of input files using pathname expansion.
inFiles=(wnd*.grb)

# Pass the input-files array to another command and read its output
# - line by line - into a new array, `outFiles`.
# The example command here simply prepends 'out' to each element of the 
# input-files array and outputs each (modified) element on its own line.
# Note: The assumption is that the filenames have no embedded newlines
# (which is usually true).
IFS=$'\n' read -r -d '' -a outFiles < \
  <(printf "%s\n" "${inFiles[@]}" | sed s'/^/out-/')

# Note: If you use bash 4, you could use `readarray -t outFiles < <(...)` instead.

# Output the resulting array.
# This also demonstrates how to use an array as an argument list 
# to pass to _any_ command.
printf "%s\n" "${outFiles[@]}"