Penguin
Blame: HereDocuments
EditPageHistoryDiffInfoLikePages
Annotated edit history of HereDocuments version 7 showing authors affecting page license. View with all changes included.
Rev Author # Line
1 CraigBox 1 You may have seen scripts containing something like this:
2 <verbatim>
3 cat << EOF
4 What you are about to do is a really really really bad thing.
5 Only do this if you are absolutely sure you want to do it.
6
7 Are you absolutely sure that you want to do this? [[y/N]
8 EOF
9 </verbatim>
10 (Well, you won't have seen anything like that, because the UnixWay is to shoot first and never ask questions!)
11
12 This is what is called a __Here Document__. The << operator instructs the shell to read your typed input and use all those lines as interactive input to the command that occurred before the <<.
13
14 Here documents are handy for issuing multi-line commands to *interactive* programs such as sed, ftp, cat etc.
15
5 BenStaz 16 The format is:
17 <verbatim>
1 CraigBox 18 <<[[-] word
5 BenStaz 19 Line 1
20 Line 2
21 Line 3
22 delimiter
23 </verbatim>
1 CraigBox 24
25 Almost always it is the case that delimiter=word but it is possible that ''word'' and ''delimeter'' may differ (especially if any of the characters in ''word'' are quoted), but that's incredibly rare. If you need to know the intracacies, see the bash page. If you put a - after the << then all leading tab characters are stripped from the input lines; this means you can lay out commands as you would normally and they will all be entered without the tabs.
26
27 A variant on the here document is the [Here String|HereStrings] which uses three <'s to expand a word and supply it to the command on stdin.
28
7 CraigBox 29 <pre>
1 CraigBox 30 foo <<< word
7 CraigBox 31 </pre>
1 CraigBox 32
33 Why would you use such a thing? For documentation, compare:
34
7 CraigBox 35 <pre>
36 cat << EOF
37 foo bar
38 foo bar
39 bar foo
40 bar baz foo
41 foo bar foo
42 EOF
43
44 echo foo bar
45 echo foo bar
46 echo bar foo
47 echo bar baz foo
48 echo
49 echo foo bar foo
50 </pre>
1 CraigBox 51
52 Then compare that in a twenty line block. Makes writing the documentation easier and checking it painless (did you mean to mention 'echo' at the beginning of a line and your mind skipped over it because all the lines began with echo?)
53
54 Another very important use is automating commands such as FTP that don't otherwise provide a method for scripting;
4 BenStaz 55 <verbatim>
1 CraigBox 56 ftp -n <<EOF
57 open ftp.example.org
58 user anonymous ${USER}@
59 cd /path/to/file
60 binary
61 passiveDocum
62 get file.gz2
63 EOF
4 BenStaz 64 </verbatim>
7 CraigBox 65
1 CraigBox 66 See bash(1).
6 BenStaz 67
68 So Remember, if you ever need to automate an interactive bash program keep Here Documents in mind.
69 BUT Don't forget that sometimes it's easier to have an already made file containing all the commands!
70
71 eg:
72
73 * ftp -n < ftp_instructions

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach()