Sending multiple lines of input to a program

Contributor Icon Contributed by Rex  
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

 

4 Comments -


No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment -