echo with a here-document


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting echo with a here-document
# 1  
Old 08-19-2005
echo with a here-document

I do not know how valid this post is, but here it is.

I was writing a script for testing some development code that I had written. It involved many echo statements to redirect the output into a log file.

Given the large number of echo statements, I took to this solution

Code:
cat <<EOF >> /tmp/log
A Line in the log file.
The next line in the log file.
And the last line in the log file.
EOF

which will produce

Code:
$ cat /tmp/log
A Line in the log file.
The next line in the log file.
And the last line in the log file.

Since there were many occurences of these cat command (and also being a case of UUOC), I thought of using the here-document in the following manner to arrive at the same solution as above.

Code:
echo <<EOF >> /tmp/log
A Line in the log file.
The next line in the log file.
And the last line in the log file.
EOF

But in this case, /tmp/log was empty.

Look at the man pages of sh and ksh, it says
Code:
ksh
       << marker
              after  reading the command line containing this kind of redirec-
              tion (called a here document), the shell copies lines  from  the
              command  source  into  a  temporary  file  until a line matching
              marker is read.  When the command is executed, standard input is
              redirected  from  the  temporary  file.

Code:
sh
   Here Documents
       This  type  of  redirection  instructs the shell to read input from the
       current source until a line containing  only  word  (with  no  trailing
       blanks)  is seen.  All of the lines read up to that point are then used
       as the standard input for a command.

Basically, the here document would be written into some temporary file and then re-read. If that be the case, why doesnt the echo with here documents work ?

I went through the The Open Group Base Specifications Issue 6 - UNIX Specification for echo and here-document. They dont say anything in particular about this.

Opinions/suggestions/remarks are welcome.

Thanks,
vino

Last edited by vino; 08-19-2005 at 09:35 AM..
# 2  
Old 08-19-2005
Hey vino,

I also have in my installation script many lines that should be inserted into a file.
I did it in the following way:

Code:
echo "A Line in the log file.
The next line in the log file.
And the last line in the log file." > /tmp/log

Nir
# 3  
Old 08-19-2005
I assume its because echo (unlike most unix commands) does not 'by default' read from the stdin descriptor. All these commands simply echo a new line:

a) echo "mystring" | echo
b) cat myfile | ehho
c) echo < myfile
d) echo <<EOF
blah1
blah2
EOF

So in your second example:
Quote:
Originally Posted by vino
echo <<EOF >> /tmp/log
A Line in the log file.
The next line in the log file.
And the last line in the log file.
EOF
you are in effect doing:

echo < myinfile > myoutfile

it's just the shell is creating the file for you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to echo within EOF (here-document)

ssh $USR@$host /bin/bash <<\EOF >> ship-error.txt awk 'BEGIN{f=0} !f { s=$0; sub(/,.+/, "", s); gsub(//, " ", s); t=(systime()-mktime(s)); if(t<=14400) f=1 } f ' /logs/shiperror.log EOF I want to use an echo command within the EOF. Is this possible? something like this: So that it prints... (3 Replies)
Discussion started by: Deepthz
3 Replies

2. Shell Programming and Scripting

Here document inside a here document?

Can we use a here document inside a here document? Something like this ssh user@remotehost << REMOTE sudo vserver vsernamename enter << VSERVER perform actions on vserver. VSERVER REMOTE (6 Replies)
Discussion started by: mnanavati
6 Replies

3. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

4. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

5. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

6. Shell Programming and Scripting

Variables in HERE DOCUMENT

Here is my problem: can we set the variables in the HERE DOCUMENT? I have tried but failed. Any one has good comments please let me know. #!/bin/csh -f pbrun -u xmgbrk runshell <<! @ $num1 = `wc -l /home/tpltp/csh/scripts/who.csh` echo "$num1" if ( $num1 > 0 ) then echo $num1 | tee -a... (1 Reply)
Discussion started by: tpltp
1 Replies

7. Shell Programming and Scripting

Document

Plz can somebody give me the shell and perl scripting documents,i need to start the scrpts learning.now i know about the linux commands,but need help in putting the same in the scripting with do,if,while and also using diffrent commands in the scrpipts,pls help.. (3 Replies)
Discussion started by: satish.res
3 Replies

8. Shell Programming and Scripting

ssh and here document

Hi :) how can I use here doc to use ssh? I am facing a problem with the below script: #!/bin/bash ssh hosein@localhost << * 123456 * "123456" is my password Thanks (2 Replies)
Discussion started by: htabesh
2 Replies

9. Shell Programming and Scripting

The here document <<

Hello, I want to know the use of the here document with the << operator. Tried to go through some books but the concept was not clear. Please can any1 expalin me this with a simple example. Thanks, Rahul. (6 Replies)
Discussion started by: rahulrathod
6 Replies

10. UNIX for Dummies Questions & Answers

Teaching myself Here Document

I understand that a Here Document will redirect all of the lines between the beginning marker for the here document and the ending marker into the command specified just as if the text were coming from standard input. I am trying to understand the Here Document with this example: # Menu file... (3 Replies)
Discussion started by: ericelysia
3 Replies
Login or Register to Ask a Question