looping object as inputs in BASH, SHELL script

2019-09-10 09:11发布

multiple files with the same name grid_200001.grb2.nc with only one components changing and that is the month grid_200010.grb2.nc. I would like to use them all simultaneously as input files

An example of what I am hoping to achieve is

fname="grid_"
year="2010"
month="01 02 03 04 05 06 07 08 09 10"
ext="grb2"
end="nc"

for((y=$year;y<=$year;y++));
do
    for m in $month
    do

    ifile=$fname$y$m.$ext.$end

    >>merge $ifile $ifile ... ofile 

example of the desired command i would like to have is

 >> merge grid_200001.grb2.nc grid_200002.grb2.nc ....grid_200012.grb2.nc ofile

I would like all the files i have available as input files inputs at the same time

3条回答
Luminary・发光体
2楼-- · 2019-09-10 09:33

You can try this:

cat grid_2010[01][0-9].grb2.nc >> ofile
查看更多
时光不老,我们不散
3楼-- · 2019-09-10 09:42

I'm not sure I understand the question exactly (edited thanks to your comments)

filenames=""

for year in $(seq 2000 2010)
do 
    for month in $(seq -w 0 10)
    do 
        filename="grid_"$year$month".grb2.nc"
        filenames=$filenames" "$filename
    done
done
your_command $filenames

which may be what you need? cat and wildcards is nice but would it keep the same order?

edited again: But then, the wildcard choice is much simpler your_command grid_20[01][0-9][01][0-9].grb2.nc if you need them only in ascending order (will that always be the case with the wildcards?)

查看更多
家丑人穷心不美
4楼-- · 2019-09-10 09:47
fname="grid_"
years=({2000..2010})
months=(01 02 03 04 05 06 07 08 09 10)  ## ({01..10}) should be the same
ext="grb2"
end="nc"

shopt -s extglob
IFS='|' eval 'pattern="${fname}@(${years[*]})@(${months[*]}).${ext}.${end}"'
echo "pattern = ${pattern}"  ## for curiosity.
files=($pattern)  ## pathname expansion is sorted in Bash.

merge "${files[@]}" ofile
查看更多
登录 后发表回答