Have you ever pasted something into vi or vim and it just looks wrong, every line has extra indents?
What if you dont have nano, or emacs to copy a said script another way?
Well you can always use these 2 next methods.
How to write a file with CAT and REDIRECTION
#############################################
2 ways:
cat redirection method
————————-
cat > file.out
contents1
contents2
CONTROL-C
NOTE: line where control-C is pressed doesnt save to file
Here document method
———————
cat > file.out << EOF
contents
contents1
contents2
EOF
contents3
NOTE: need to escape \,$ and ` characters
NOTE: EOF needs to be by itself on a sepearte line
NOTE: it doesnt have to be the letters EOF, it can be
NOTE: contents3
BEST WAY IF YOU DONT NEED TO DO A SCRIPT
##########################################
I dont know the name of this method, so i will just call it “cat redirection method”
NOTE: Im doing everything in Putty, so it might be different for you
Pro: Dont need to escape characters, Best method for Pasting
Con: Cant be part of script because you need to press CONTROL-C
Type “cat > file.out” and hit ENTER
Then enter text or paste text (right click copied text from clipboard – if using putty). The line on which you press CONTROL-C will not be included, so make sure you have one extra newline (an empty one) and press CONTROL-C. Sidenote on pasting: If pasting dont forget to press ENTER again so that your cursor is on a newline, the proceed with CONTROL-C, as the general pattern for copying is to only select the text that you want, when you paste your usually missing that extra needed newline character.
(Press CONTROL-C HERE)
cat > file.out
stuff stuff
stuff stuff
Just showing there are 2 ways to do it:
\dogs
\dogs
\ i want 1 back slash
\ i want 1 back slash
\\ i want 2 back slash
\\ i want 2 back slash
\\\ i want 3 back slash
\\\ i want 3 back slash
\\\\ i want 4 back slash
\\\\ i want 4 back slash
$pwd
`date` (dont press CONTROL-C here, but on the next line down or else the line with `date` will not get included)
(So Press CONTROL-C here and this line will not get included – which is what we want for this demonstration)
The result/output is this:
# cat file.out
stuff stuff
stuff stuff
Just showing there are 2 ways to do it:
\dogs
\dogs
\ i want 1 back slash
\ i want 1 back slash
\\ i want 2 back slash
\\ i want 2 back slash
\\\ i want 3 back slash
\\\ i want 3 back slash
\\\\ i want 4 back slash
\\\\ i want 4 back slash
$pwd
`date` (dont press CONTROL-C here, but on the next line down or else the line with `date` will not get included)
NOTE: Dont paste in “cat > file.out” with the file contents, run the “cat > file.out” seperately.
HERE DOCUMENTS
#################
Here-Docs are the proper term for this.
More info: http://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash
Pro: Can be part of a script and copy pasted
Con: Need to escape special bash characters (You need to escape \ and $ and ` characters)
NOTE ON ESCAPING ESCAPE CHARACTER \: escaping a back slash is confusing, well its just that you can do it 2 ways, with an odd or even number of back slashes. Take the number of backslashes you need and multiply by 2, thats how many you really need to put. So If I need 3 backslashes in ouput ill put down 6. However you can also do minus 1, so 5 backslashes will also result in 3 backslashes output.
With incorrect escaping of special characters:
cat > file-WRONG << EOF
stuff stuff
stuff stuff
Just showing there are 2 ways to do it:
\dogs
\dogs
\ i want 1 back slash
\ i want 1 back slash
\\ i want 2 back slash
\\ i want 2 back slash
\\\ i want 3 back slash
\\\ i want 3 back slash
\\\\ i want 4 back slash
\\\\ i want 4 back slash
$pwd
`date`
EOF
With correct escaping of special characters:
cat > file-CORRECT << EOF
stuff stuff
stuff stuff
Just showing there are 2 ways to do it:
\\dogs
\dogs
\\ i want 1 back slash
\ i want 1 back slash
\\\\ i want 2 back slash
\\\ i want 2 back slash
\\\\\\ i want 3 back slash
\\\\\ i want 3 back slash
\\\\\\\\ i want 4 back slash
\\\\\\\ i want 4 back slash
\$pwd
\`date\`
EOF
NOTE: if I used >> file instead of < file, yup you guessed it would of appended to file
OUTPUT OF file-CORRECT and file-WRONG:
# cat file-WRONG
stuff stuff
stuff stuff
Just showing there are 2 ways to do it:
\dogs
\dogs
\ i want 1 back slash
\ i want 1 back slash
\ i want 2 back slash
\ i want 2 back slash
\\ i want 3 back slash
\\ i want 3 back slash
\\ i want 4 back slash
\\ i want 4 back slash
Sat May 24 14:44:20 PDT 2014
# cat file-CORRECT
stuff stuff
stuff stuff
Just showing there are 2 ways to do it:
\dogs
\dogs
\ i want 1 back slash
\ i want 1 back slash
\\ i want 2 back slash
\\ i want 2 back slash
\\\ i want 3 back slash
\\\ i want 3 back slash
\\\\ i want 4 back slash
\\\\ i want 4 back slash
$pwd
`date`
NOTE ABOUT REDIRECTION:
#######################
NOTE: if did >> file.out instead of > file.out, then text would of been appended to file.out. With > file.out a file is created, and if a file exists it will truncate the old file (remove contents of old file – keeps file attributes permissions) With >> file.out a file is create, and if a file exists it will just append to the existing output.