Information on heredoc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Information on heredoc
# 1  
Old 01-07-2018
Information on heredoc

Hi ,

I am using ksh on Aix 7.1 and found the below code in a script which is a bit confusing to me, any help will be appreciated

Code:
<<comments
put ${pathforfiles}/${ftpfilename}
put ${logs}/${filename}.sch
put ${logs}/${filename}.tgr
comments

I have searched for information on heredoc but did not find any article that says what would happen if there is no command to which heredoc is passed

Thanks
# 2  
Old 01-07-2018
Shell Command Language
This indicates that it writes to stdin by default.
Nested here documents example:

Code:
#!/bin/bash
$cat <<eofa; cat <<eofb
$>>test1
$>>test2
test1 
test2

$<<eofa; cat <<eofb
$>>test1
$>>test2

test2

In the second block the first instance does not have stdin defined as an output device, and so nothing is there to output or process the data "test1".

I think Don Cragun probably knows more.
# 3  
Old 01-07-2018
This is cute...

There is a comment in the POSIX Standard's rationale for the simple commands section of the shell command language description that says:
Quote:
An example of redirections without a command name being performed in a subshell shows that
the here-document does not disrupt the standard input of the while loop:
Code:
IFS=:
while read a b
do	echo $a
	<<−eof
	Hello
	eof
done </etc/passwd

But, I don't see anything in the standard that clearly specifies for what a here-document without a command is providing input. With the command exec a here-document does replace standard input for the current shell execution environment.

Playing around a little bit with the above example code in the POSIX rationale and the code supplied in post #1 in this thread, it appears to me that the here-document:
Code:
<<comments
put ${pathforfiles}/${ftpfilename}
put ${logs}/${filename}.sch
put ${logs}/${filename}.tgr
comments

is a shell language comment similar to the C language comment:
Code:
/*
put ${pathforfiles}/${ftpfilename}
put ${logs}/${filename}.sch
put ${logs}/${filename}.tgr
*/

except that any expansions occurring in that here-document may have side effects. For example:
Code:
count=0
IFS=:
while read a b
do	echo $a
	<<−comment
	This comment counts lines read from the input file
	$((++count))
	comment
done </etc/passwd
echo "$count lines were read from /etc/passwd"

might produce abbreviated output similar to:
Code:
root
nobody
daemon
...
108 lines were read from /etc/passwd

If you wanted a multi-line shell comment without side effects (like the C example I showed earlier) you could quote one or more characters in the EOF string of the here-document:
Code:
count=0
IFS=:
while read a b
do	echo $a
	<<−"comment"
	This comment counts lines read from the input file
	$((++count))
	comment
done </etc/passwd
echo "$count lines were read from /etc/passwd"

which would then produce abbreviated output similar to the following (with the last line being exactly like that shown below):
Code:
root
nobody
daemon
...
0 lines were read from /etc/passwd

Are we having fun yet? Smilie
These 3 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. What is on Your Mind?

Not enough information...

I appreciate the fact that rules are for everyone and I screwed up by not putting a screen printout in code brackets... However, I was a bit dismayed when I received the following and tried to reply to say "Thank you" for sending me the reminder. I could not reply and had no idea what the... (4 Replies)
Discussion started by: RogerBaran
4 Replies

2. Shell Programming and Scripting

How To Concatenate Two Commands in script using heredoc?

Hello, I am trying to place two commands in heredoc below is the snippet if ;then actionOnTux="$actVerb" else actionOnTux="$actVerb" fi echo "Performing ACTION: $action on $tux@$srv .....\n" if ; then ... (5 Replies)
Discussion started by: kataria.anand
5 Replies

3. Shell Programming and Scripting

Echo multi-line string via heredoc syntax

$ cat bashtest #!/usr/local/bin/bash echo <<<"EOF" line1 line2 line3 EOF $ ./bashtest ./bashtest: line 3: line1: command not found ./bashtest: line 4: line2: command not found ./bashtest: line 5: line3: command not found ./bashtest: line 6: EOF: command not found What am i doing... (4 Replies)
Discussion started by: urello
4 Replies

4. Shell Programming and Scripting

Calling sqlplus from Korn shell heredoc issue

Hi, I am facing an issue wherein some temporary files (here docs) are getting created in /tmp and are not getting deleted automatically. When i check the list of open files with below command i can see one file is getting appended continuously.(In this case /tmp/sfe7h.34p) The output is... (4 Replies)
Discussion started by: Navin_Ramdhami
4 Replies

5. Shell Programming and Scripting

Variables in heredoc

I currently use this message to send e-mails in a script but I would also like to save the output of this code to a file as well while preserving the variables. What's the easiest way to accomplish this? #Sending mail notification when=`/bin/date` /usr/sbin/sendmail -t >2 <<-EOM... (2 Replies)
Discussion started by: woodson2
2 Replies

6. Shell Programming and Scripting

heredoc error check

Hi, Is there any way to check the error in heredoc? Code: /export/opt/SCssh/3.7.1_C0/bin/sftp -B - csi@192.168.1.100 <<FTP lcd /WEBSERVER_LINK/data_logs/ ls pub/csidata/GeneralAppFields_8_1_Feed.out get pub/csidata/GeneralAppFields_8_1_Feed.out quit FTP Now I want to check... (1 Reply)
Discussion started by: bheeshmaraja
1 Replies

7. Shell Programming and Scripting

Problem in SFTP using heredoc

Hi, I'm having heredoc to get files from ftp server. #!/bin/bash /export/opt/SCssh/3.7.1_C0/bin/sftp csi@192.168.1.100 <<GET_FILES lcd /WEBSERVER_LINK/data_logs/ ls -l pub/csidata/GeneralAppFields_8_1_Feed.out pub/csidata/CtcCatalog_7_3_3_Feed.out get... (13 Replies)
Discussion started by: bheeshmaraja
13 Replies

8. Shell Programming and Scripting

HEREDOC with fdisk

Hi folks What I'm trying is to build a partitioning script. I can pass a HEREDOC to fdisk just fine. Like this: fdisk /dev/sda << EOF p q EOF but I don't know how to put that HEREDOC into a varible to pass it to fdisk. This is what I have tried so far (no luck) #!/bin/bash ... (3 Replies)
Discussion started by: latenite
3 Replies

9. UNIX for Dummies Questions & Answers

Information

I'm new to unix and wanted to know if there are UNIX tools/programs on the market that can be loaded onto my laptop or external hard drive that I can use to practice and test what I'm learning. :) (2 Replies)
Discussion started by: Xman0ne
2 Replies
Login or Register to Ask a Question