Sample Header Ad - 728x90

Empty string breaks command line

3 votes
4 answers
1022 views
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.
Asked by Brendan (29 rep)
Oct 24, 2022, 12:43 PM
Last activity: Oct 31, 2022, 10:44 AM