Sending multiple lines of input to a program

Contributor Icon Contributed by Rex Date Icon October 20, 2003  
Tag Icon Tagged: UNIX shell scripting

Using a ‘here document’ it is possible to send multiple lines of input to a program from a shell script.


A here document uses the << I/O redirector followed by a code. The subsequent lines are redirected to the program until the specified code. The basic syntax is:

program << CoDe
Line of input
More input

extra input
CoDe

The code needs to be something that would not occur in the text. This technique is useful for working with interactive programs like ftp. In the following example, a file specified in a shell variable ($filename) is retrieved from an ftp server:

filname=important.file.gz
ftp -n ftp.server.name << TheEnd
user username password
cd directory/subdir
get $filename
quit
TheEnd

Previous recipe | Next recipe |
 

Viewing 2 Comments

 
close Reblog this comment
blog comments powered by Disqus