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
2010-06-01 20:31:28
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
2010-06-01 20:35:06
it was just an example with a given command
Aif
2010-06-01 21:05:40
This does not even come close to answering the question.
Dennis Williamson
2010-06-02 04:21:31
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
2010-06-01 21:02:53
Don't need the parentheses: `cat << END > afile` followed by the heredoc works perfectly well.
glenn jackman
2010-06-02 00:12:03
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
2010-06-07 17:53:45
+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
2010-06-02 03:40:50