Solved

Unix EOF

Posted on 2010-08-17
3
1,230 Views
Last Modified: 2012-05-10
Hi,

I have something in my code as  
PGNWCC01 <<EOF
nwccpr01
EOF
Can u please explain me the above code snippet?
0
Comment
Question by:r4ranjor
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 33461649
0
 
LVL 11

Expert Comment

by:Pieter Jordaan
ID: 33461970

 << means that input follows
EOF tells it to continue reading until it gets another EOF.

You can change it to anything, but it has to be unique. If there is an EOF anywhere else in the text that follow, it will stop reading.

This will also work:
PGNWCC01 <<READLINES
nwccpr01
READLINES
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 33477955
A simple tutorial on UNIX "Here is" Command

A here document is used to redirect input into an interactive shell script or program.

We can run an interactive program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script.

The general form for a here document is:

command << delimiter
document
delimiter
 

Here the shell interprets the << operator as an instruction to read input until it finds a line containing the specified delimiter. All the input lines up to the line containing the delimiter are then fed into the standard input of the command.

The delimiter tells the shell that the here document has completed. Without it, the shell continues to read input forever. The delimiter must be a single word that does not contain spaces or tabs.

Following is the input to the command wc -l to counto total number of line:

[amrood]$wc -l << EOF
      This is a simple lookup program
      for good (and bad) restaurants
      in Cape Town.
EOF
3
[amrood]$
 

You can use here document to print multiple lines using your script as follows:

#!/bin/sh

cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF      
 

This would produce following result:

This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
 

The following script runs a session with the vi text editor and save the input in the file test.txt.

#!/bin/sh

filename=test.txt
vi $filename <<EndOfCommands
i
This file was created automatically from
a shell script
^[
ZZ
EndOfCommands
 

If you run this script with vim acting as vi, then you will likely see output like the following:

[amrood]$ sh test.sh
Vim: Warning: Input is not from a terminal
[amrood]$
 

After running the script, you should see the following added to the file test.txt:

[amrood]$ cat test.txt
This file was created automatically from
a shell script
[amrood]$
 
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now