![]() |
Need an explanation of here scripts as well as the "cat" command
Hi everyone!
I am currently in the very early stages of learning how to use the shell and write scripts (and I mean really early - only been at it for 3 days or so). Now I am following a tutorial, and I have reached a section about the so-called "here scripts" or "here documents". In the tutorial, we have the following script: Code:
#!/bin/bashCode:
#!/bin/bashSo would anyone care to explain to me what it does in this specific script as well as alternative applications? (And please refrain from just quoting the man-pages.. I have read them multiple times, and I still just don't get it - It's probably very simple, but please bear with me!) Thank you in advance! |
Basically it tells the shell that you are going to enter a multi-line string until "_EOF_". You could call it anything you want, not just EOF or STOP.
Kind regards |
You can also do variable substitution within the document. This should illustrate it nicely:
Code:
#!/bin/bash./script Martin $1 is called a positional parameter as it returns the first parameter provided with the script. |
So I find it interesting that you say you are doing a tutorial and yet it tells you what to do but nothing about the why (odd).
Anyway, this may help explain further. |
Quote:
|
So that explained the here document... Now the second part: What exactly does the "cat" command do, and why does it produce the same output as "echo" in this case? (Again, I have read the man pages, though I didn't quite understand it.. Perhaps one of you would be so kind as to dumb it down? Remember, I only started learning this 3 days ago, so go easy!)
|
Did you even read the link?
Quote:
|
Quote:
I apologize if there is anything I overlooked in the link, but I don't see any explanations of the cat command - only what the here scripts are and what to do with 'em :) Again, thanks for your time! |
Cat very simply copies standard in (or a file) to standard out. Think of it as a kind of pipe version of echo. It prints out everything you feed into it.
It's often used to "concatenate" several files together, as sending multiple inputs into it will give you one continuous output, but it's very useful in scripting as well. It can do a few filtering actions too. Read the man page. A heredoc simply sets a block of text apart, as if it existed in a separate file, and spits it out as if you were using a < redirect. Using cat with it thus prints the contents. Note that cat is an external command, /bin/cat, while the heredoc is a shell built-in feature. |
Quote:
And thanks to everyone else who answered as well :) SOLVED |
Glad to help out. Keep on scripting!
Come to think of it, I wonder why bash has never bothered to create a built-in version of cat. You'd think it would be useful not to have to spawn an external process just to print out some stuff, and it should be easy enough to implement. Does any other shell have a cat built-in? It might then even be possible to set it up so that redirections without receiving commands, like "<file.txt" or a heredoc alone, would go directly to stdout, as if there were an implied cat in front of it (controllable with a shell option, of course). Sorry, just thinking out loud. ;) |
Quote:
Quote:
|
| All times are GMT -5. The time now is 12:56 PM. |