Question

I am quite STUMPED by why on earth the following bash script would hang in CentOS6.4

/bin/cat >> /root/heredoc <<EOF

    if [ $(grep -c "$IP_ADDR\|$HOSTNAME" $HOSTS_FILE) -ne 0 ]; then
EOF

Don't worry if the contents of the here doc doesn't make sense. I simply took out the line of code that makes the script hang. I am definitely lacking some insight into how bash handles here docs. It's not the pipe (vertical bar) as it still hangs without the pipe.

Était-ce utile?

La solution

I want the content as a literal string. No processing or evaluation of the content is desired.

if you want the content as literal text, try this:

/bin/cat >> /root/heredoc << 'EOF'

    if [ $(grep -c "$IP_ADDR\|$HOSTNAME" $HOSTS_FILE) -ne 0 ]; then
whatever you have

EOF

note, single quoted the heredoc 'EOF'

here is an example:

kent$  cat << eof
$(echo $HOME)
eof
/home/kent

kent$  cat << 'eof'
$(echo $HOME)
eof
$(echo $HOME)

Autres conseils

The problem is $( grep ... ). A HERE doc is by default interpreted as double quoted string which means the grep is executed. $HOSTS_FILE is probably empty, so grep is waiting for standard input (try pressing Ctrl+D).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top