Visit Our UNIX and Linux User Community

Full Discussion: echo with a here-document
Top Forums Shell Programming and Scripting echo with a here-document Post 81403 by vino on Friday 19th of August 2005 08:19:28 AM
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..
 
Test Your Knowledge in Computers #5
Difficulty: Easy
The UNIX trademark is a "certification mark".
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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

Featured Tech Videos

All times are GMT -4. The time now is 10:11 AM.
Unix & Linux Forums Content Copyright 1993-2020. All Rights Reserved.
Privacy Policy