3

I was a running a bash script fine on one of the linux host(bash version version 3.2.25(1)), since I have moved the script to another host (bash verison version 4.2.25(1)) it is throwing warning as

line 36: warning: here-document at line 30 delimited by end-of-file (wanted `EOM') (wanted `EOM')

The code in question is:-(not sure how EOM works)

USAGE=$(cat <<EOM
Usage:${BASENAME} [-h] [-n] [-q]
-h, --help        This usage message
-n, --dry-run     
-q, --quiet       
-d, --Destination 
EOM)

}

I have made sure there is no space, tab or any special symbols before and after EOM as it was cause of error find during research on google.

bash (bash -x) debugged output looks like:-

+ source /test/test1/script.sh
./test.sh: line 36: warning: here-document at line 30 delimited by end-of-file      
(wanted `EOM')
++ cat
+ USAGE='Usage:test [-h] [-n] [-q]
-h, --help        This usage message
-n, --dry-run     
-q, --quiet       
-d, --Destination

This is sourcing a script.sh where usage is used in one of the function as: (However i guess this is not the cause of the error but may be I am wrong)-

show_usage()
{

declare -i rc=0
show_error "${@}"
rc=${?}
echo "${USAGE}"
exit ${rc}  
}  

Please help and getting rid of this warning and how this EOM working here exactly?

Ruchir Bharadwaj
  • 1,132
  • 4
  • 15
  • 31
  • @tripleee The dupe target isn't great, as it contains seemingly correct code in the question and all the answers are just guessing what the reason might be, whereas in this question, the reason why the here-doc fails is obvious. Just bumped into it when looking for a dupe for https://stackoverflow.com/questions/44030479/multiple-command-for-bash-shell-script; this question here seems much better than its dupe target. – Benjamin W. May 17 '17 at 16:59

2 Answers2

10

When using a here-document, make sure the delimiter is the only string on the line. In your case, the right parenthesis is taken as part of the word, so the matching delimiter is not found. You can safely move it to the next line:

USAGE=$(cat <<EOM
Usage:${BASENAME} [-h] [-n] [-q]
-h, --help        This usage message
-n, --dry-run     
-q, --quiet       
-d, --Destination 
EOM
)
choroba
  • 231,213
  • 25
  • 204
  • 289
3

While this does not really answer you question, have you considered using read instead of cat?

read -d '' usage <<- EOF
Usage:${BASENAME} [-h] [-n] [-q]
-h, --help        This usage message
-n, --dry-run     
-q, --quiet       
-d, --Destination 
EOF

echo "$usage"

It reads the content of the here string into the variable usage, which you can then output using echo or printf.

The advantage is that read is a bash builtin and thus faster than cat (which is an external command).

You can also simply embed newlines in a string:

usage="Usage:${BASENAME} [-h] [-n] [-q]
-h, --help     This usage message
...
"
chepner
  • 497,756
  • 71
  • 530
  • 681
helpermethod
  • 59,493
  • 71
  • 188
  • 276