I am having trouble trying to describe my issue. Please bear with me. I have a script that calls a command. I need to optionally include an extra argument in the command line depending on the input file. I tried this:
case "$model" in
CNRM-CM6-1|CNRM-ESM2-1)
trim_op="-selindexbox,2,361,2,293"
;;
EC-Earth3)
trim_op="-selindexbox,2,361,2,291"
;;
IPSL-CM6A-LR)
trim_op="-selindexbox,2,361,2,331"
;;
MPI-ESM1-2-HR)
trim_op="-selindexbox,2,801,3,403"
;;
MPI-ESM1-2-LR)
trim_op="-selindexbox,2,255,2,219"
;;
NorESM2-LM)
trim_op="-selindexbox,2,359,2,383"
;;
*)
trim_op=""
;;
esac
cdo -O remapcon,"$target_grid" "$trim_op" "$input_file" "$output_file"
but bash chokes on the empty word. What is the proper way of doing such a thing in bash? What I ended up doing was:
if [[ -z $trim_op ]] ; then
cdo -O remapcon,"$target_grid" "$input_file" "$output_file"
else
cdo -O remapcon,"$target_grid" "$trim_op" "$input_file" "$output_file"
fi
I am feeling quite ignorant right now. Is there a name for this? Every search I make turns up getop(s) which is not what I am looking for.
"$trim_op"quoted exactly like this in your actual code whencdois called? This would callcdowith an empty 2nd argument. It is unclear whether the issue is thatcdocan't be called with an empty 2nd argument, or whether you want to remove the argument if it's empty.$ ls "". So I assumed it was not a cdo issue, per se.lsutility does not like an empty argument because there is no file or directory that has an empty name.