views:

205

answers:

3

How can I write a here document to a file in bash script?

A: 
#!/bin/bash
wall <<stuffhere
hello everybody, reboot in 5 minutes
stuffhere

should do the work. Otherwize, you can consider using a temporary file using > >> and < operators.

Aif
This just sends it to all logged in users, doesn't it? I need to write the here doc to a file lets say X. The > (and related) operators don't work when placed after the here doc.
Josh
it was just an example with a given command
Aif
This does not even come close to answering the question.
Dennis Williamson
A: 

For future people who may have this issue the following format worked:

(cat <<- _EOF_
        LogFile /var/log/clamd.log
        LogTime yes
        DatabaseDirectory /var/lib/clamav
        LocalSocket /tmp/clamd.socket
        TCPAddr 127.0.0.1
        SelfCheck 1020
        ScanPDF yes
        _EOF_
) > /etc/clamd.conf
Josh
Don't need the parentheses: `cat << END > afile` followed by the heredoc works perfectly well.
glenn jackman
Thanks, this actually solved another issue I ran into. After a few here docs there was some issues. I think it had to do with the parens, as with the advice above it fixed it.
Josh
+1  A: 

Read the Advanced Bash-Scripting Guide Chapter 19. Here Documents.

Here's another example, which will write the contents to a file at /tmp/yourfilehere

cat << 'EOF' > /tmp/yourfilehere
These contents will be written to the file.
EOF

Note that the last 'EOF' should not have any whitespace in front of the word. That becomes inside of shell scripts where you may be using indentation.

Stefan Lasiewski
Thank you for your reply.
Josh