I have the following shell script (myscript.sh) for processing of multiple .grb files to convert to .nc (netCDF) format. I have placed this in the same folder where the .grb files are located and ran "myscript.sh" like below.

List of .grb files in the "Binary" folder are like below:

NLDAS_VIC0125_H.A19901231.0000.002.grb
NLDAS_VIC0125_H.A19901231.0100.002.grb
.
.
NLDAS_VIC0125_H.A19901231.0500.002.grb 

~Downloads/Binary$ bash myscript.sh

#!/bin/bash
for i in 000 100 200 300 400 500
cdo -f nc copy NLDAS_VIC0125_H.A19901231.0{i}.002.grb NLDAS_VIC0125_H.A19901231.0{i}.002.nc
done

My script gives me an error:

script.sh: line 3: syntax error near unexpected token `cdo'
script.sh: line 3: `cdo -f nc copy NLDAS_VIC0125_H.A19901231.0{i}.002.grb NLDAS_VIC0125_H.A19901231.0{i}.002.nc'

Can somebody help me to correct this?

Any help is appreciated.

Thanks,

  • At the end of the command is a ' instead of a ` – ADDB Jun 28 '17 at 23:42
up vote 1 down vote accepted

Your loop is missing a do and your variables are missing their de-referencing $:

#!/bin/bash
for i in 000 100 200 300 400 500
do
  cdo -f nc copy NLDAS_VIC0125_H.A19901231.0${i}.002.grb NLDAS_VIC0125_H.A19901231.0${i}.002.nc
done

However if you want to loop over all the .grb files in the directory it would be simpler to use a * wildcard, and derive the nc filename from the .grb one by removing and replacing its extension

#!/bin/bash
for file in *.grb
do
  cdo -f nc copy "$file" "${file%.grb}.nc"
done

Also, it's better practice to make the script executable and simply call it by name

chmod +x myscript.sh

then

./myscript.sh

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.