[BASH] here document creates problem with if-fi statemens.
Hi guys!
I am reading the Advanced Bash Scripting and I decided to practice a bit and create my own little script. I am trying to insert a little here document within an if-fi statement but i get a little problem: Code:
if [ $0 = "mountemall" ]; then when you put the << to start the here document everything gets red bellow that point until you put the keyword. But in my case even though I put the keyword as I showed you the fi and everything below it stays in red as if it were commented... now if I do this: Code:
if [ $0 = "mountemall" ]; then Why does that happens? if you want I can post the whole code in case the problem can be within the code itself. |
Since the redirect into `cat`, once opened, will stay open until the process completes (or some other error occurs), you only need a single < to accomplish the task.
I tried your first example with only a single < and it works for me. Sasha |
Quote:
as you can see I was doing: Code:
cat <<SUCCESS Code:
In the other hand I think that if you do "cat <SUCCESS" it will try to open a file called "SUCCESS" which doesnt exist, am I wrong on that? |
Ohhh, my goodness, that hadn't occurred to me about the tabulation of the keyword ;) nice catch!
And as for the opening of a file, if you wanted to, try this: Code:
cat << SUCCESS > filename.txt I'm not certain about your last question; try it out and see :) and let me know (it seems you're right though) Sasha |
You can still use indentation with TABs in here documents if you use the syntax <<- as in the following example:
Code:
#!/bin/bash |
exactly as expected:
Code:
#!/bin/bash Code:
[~]$ temp.sh Code:
echo Code:
cat <<END |
Quote:
One of the Here Document "gotchas" was you couldn't have a space between << (or <<-) and the delimiter word. I just checked the bash references and some examples in ABSG and that's how they show it, with no space, but Sasha's code works perfectly. A welcome change in the specification and thanks for illustrating it. |
Quote:
Code:
echo 'And |
cat << DOG > animals.txt
Quote:
Thanks -- I almost always try to look into these sorts of threads (even if I can't provide *any* input) because not only are the various documentation sometimes outdated or contain typos, but as you mentioned, sometimes things get updated, and yet are not documented anywhere! I find this << SUCCESS >> cat thread educational < as a whole > because there are < cat EOF so many different ways to > redirect things :confused: it's no wonder we get confused :p Cheers, Sasha PS - here's a novelty I didn't know of till a few weeks ago: Did you guys know there's a `tac` command? It's the opposite of `cat` in that it starts at the END (or bottom) of a file/data stream, and works backwards ;) |
Another trick in here documents is that they permit shell substitution, so that you can embed shell variables, arithmetics and results of commands inside the block of code:
Code:
#!/bin/bash Code:
$ cat testfile Code:
#!/bin/bash Code:
$ cat testfile |
[off topic]
@ colucix -- P.S. I have always liked your signature! [/off topic] |
@colucix
now, that is something cool to know!! @fruitygirl that with the tac is very interesting, I keep finding out more and more commands to do what you want to do in every single way you can think of! @catkin actually you are right! echo is the quick and dirty solution. |
Quote:
|
Quote:
|
Quote:
Code:
$ echo This is a reversed line | rev |
All times are GMT -5. The time now is 07:17 AM. |