So I have my input directory, output directory and list of files:
#!/bin/bash ## shell type
dir_in=('/Users/dossa013/data/inland-data/input/')
dir_out=('/Volumes/Macintosh HD 2/data/cmip5/cru/')
files=('/Users/dossa013/data/inland-data/input/*ts*')
where the variable "files" contains the following:
cld.cru_ts3.22.1901.2013.nc
prec.cru_ts3.22.1901.2013.nc
rh.cru_ts3.22.1901.2013.nc
temp.cru_ts3.22.1901.2013.nc
trange.cru_ts3.22.1901.2013.nc
wetd.cru_ts3.22.1901.2013.nc
What I need to do is run a command that requires as arguments an input file and an output file. In my case, the directory where the input files are located is different from the output files directory.
The caveat is: when specifying the output file, I would like to append the word "croppped" between the end of the original filename and the extension.
This loop doesn't work:
for f in ${files[@]}; do ## loop over files
echo cdo sellonlatbox "${dir_in}"${f##*/} "${dir_out}"$(printf '%s\n' "${f##*/.nc}_cropped.nc")
done
Ideally, the result would be:
cdo sellonlatbox /Users/dossa013/data/inland-data/input/prec.cru_ts3.22.1901.2013.nc /Volumes/Macintosh HD 2/data/cmip5/cru/prec.cru_ts3.22.1901.2013_cropped.nc
Where is my mistake here?
'*'
That won't expand. That's your problem. – Valentin Bajrami May 06 '15 at 09:10files=("/Users/dossa013/data/inland-data/input/"*ts*)
– Valentin Bajrami May 06 '15 at 09:19dir_in="/Users/dossa013/data/inland-data/input/"
anddir_out="/Volumes/Macintosh HD 2/data/cmip5/cru/"
. Furthermore, you need to quote every expansion e.g"${f##*/}"
and so on – Valentin Bajrami May 06 '15 at 09:49