LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2009, 01:34 PM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
Question [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

       [chunk of code]

       cat <<SUCCESS
       Operation Successful!
       >>-----------------------------
       $count files mounted correctly.
       SUCCESS
fi

       [more code]
I am using vi and this is what happens:

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

       [chunk of code]

       cat <<SUCCESS
       Operation Successful!
       >>-----------------------------
       $count files mounted correctly.
       
fi
SUCCESS
       [more code]
Then everything gets to normal color again... But i assume it will brake my if-fi statement...

Why does that happens?
if you want I can post the whole code in case the problem can be within the code itself.

Last edited by RaptorX; 08-26-2009 at 01:36 PM.
 
Old 08-26-2009, 01:40 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 08-26-2009, 01:55 PM   #3
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by GrapefruiTgirl View Post
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
Thanks for the pointer, I did found out what was wrong... and it is stupid...

as you can see I was doing:

Code:
            cat <<SUCCESS
            [text]
            SUCCESS
but vi is expecting:

Code:
            cat <<SUCCESS
            [text]
SUCCESS
for some reason vi appends the tab to the keyword and it kept looking for it... once i eliminated the space it right away worked as it should.

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?
 
Old 08-26-2009, 02:01 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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

hello
goodbye
something else

SUCCESS
NOTICE that in this case, there are a double << -- and you will get the lines catted into a file called '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

Last edited by GrapefruiTgirl; 08-26-2009 at 02:02 PM.
 
Old 08-26-2009, 02:09 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can still use indentation with TABs in here documents if you use the syntax <<- as in the following example:
Code:
#!/bin/bash
cat > testfile <<-SUCCESS
	hello world
	SUCCESS
Note there is a TAB before the limit string at the end. Blank spaces are not valid, anyway.
 
Old 08-26-2009, 02:12 PM   #6
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
exactly as expected:

Code:
#!/bin/bash
#temp file

cat <SUCCESS
this is a temp file
SUCCESS
returns:

Code:
[~]$ temp.sh
./temp.sh: line 4: SUCCESS: No such file or directory
./temp.sh: line 5: this: command not found
./temp.sh: line 6: SUCCESS: command not found
In my original code I am not using "cat <<SUCCESS > file" because I want the output to come to the stdout, im using it that way because:

Code:
echo
echo this
echo is 
echo uglier 
echo

Code:
cat <<END

than
something written like this
which is nicer...

END
And it still has the same effect. ^^

Last edited by RaptorX; 08-26-2009 at 02:16 PM.
 
Old 08-26-2009, 02:26 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by GrapefruiTgirl View Post
Code:
cat << SUCCESS > filename.txt

hello
goodbye
something else

SUCCESS
That works, which was educational!

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.
 
Old 08-26-2009, 02:30 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by RaptorX View Post
Code:
cat <<END

than
something written like this
which is nicer...

END
Code:
echo 'And
this is
sweet, too
(and doesn't start a sub-process)'
 
Old 08-26-2009, 02:40 PM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
cat << DOG > animals.txt

Quote:
Originally Posted by catkin View Post
That works, which was educational!

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 it's no wonder we get confused

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

Last edited by GrapefruiTgirl; 08-26-2009 at 02:43 PM.
 
Old 08-26-2009, 02:47 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
count=3
cat > testfile << SUCCESS
  $HOME
  $(date)
  $((++count))
SUCCESS
The content of testfile in this case will be:
Code:
$ cat testfile
  /home/alex
  Wed Aug 26 21:43:40 CEST 2009
  4
but if you embed one or more character of the limit string in quotes (double or single) the shell substitutions are not performed and the block of code is interpreted literally:
Code:
#!/bin/bash
count=3
cat > testfile << SUCCES'S'
  $HOME
  $(date)
  $((++count))
SUCCESS
In this case you will get:
Code:
$ cat testfile
  $HOME
  $(date)
  $((++count))
 
Old 08-26-2009, 02:49 PM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
[off topic]
@ colucix -- P.S. I have always liked your signature!
[/off topic]
 
Old 08-26-2009, 03:03 PM   #12
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
@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.
 
Old 08-26-2009, 03:17 PM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by GrapefruiTgirl View Post
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
No -- I didn't. One day it will come in handy ...
 
Old 08-26-2009, 03:23 PM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by GrapefruiTgirl View Post
[off topic]
@ colucix -- P.S. I have always liked your signature!
[/off topic]
Thank you! I think it fits well to many issues about computers. Indeed I still wonder how it is possible that such a microscopic piece of silicon is able to accomplish so many tasks!
 
Old 08-26-2009, 03:29 PM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by RaptorX View Post
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!
What about rev?
Code:
$ echo This is a reversed line | rev
enil desrever a si sihT
It does not work with palindromes, anyway!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [bash] code wont work - here document RaptorX Programming 6 08-17-2009 12:02 PM
a broken bash document? ninja master Linux From Scratch 10 01-29-2009 02:54 PM
Bash script creates blank file name snowman81 Programming 9 03-02-2008 10:37 PM
[BASH] Search for 5-digit numbers in document General Programming 2 01-10-2007 08:06 PM
LXer: Piracy creates jobs, FOSS creates opportunities LXer Syndicated Linux News 0 11-02-2006 11:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration