The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

Поиск:  Каталог документации

3.22. Here Documents

A here document is a way of feeding a command script to an interactive program, such as ftp, telnet, or ex. Typically, it consists of a command list to the program, delineated by a limit string. The special symbol << precedes the limit string. This has the same effect as redirecting the output of a file into the program, that is,
interactive-program < command-file
where command-file contains
command #1
command #2
...

The "here document" alternative looks like this:
#!/bin/bash
interactive-program <<LimitString
command #1
command #2
...
LimitString

Choose a limit string sufficiently unusual that it will not occur anywhere in the command list and confuse matters.

Note that "here documents" may sometimes be used to good effect with non-interactive utilities and commands.

Example 3-75. dummyfile: Creates a 2-line dummy file

#!/bin/bash

# Non-interactive use of 'vi' to edit a file.
# Emulates 'sed'.

if [ -z $1 ]
then
  echo "Usage: `basename $0` filename"
  exit 1
fi

TARGETFILE=$1

vi $TARGETFILE <<x23LimitStringx23
i
This is line 1 of the example file.
This is line 2 of the example file.
^[
ZZ
x23LimitStringx23

# Note that ^[ above is a literal escape
# typed by Control-V Escape

exit 0

The above script could just as effectively have been implemented with ex, rather than vi. Here documents containing a list of ex commands are common enough to form their own category, known as ex scripts.

Example 3-76. broadcast: Sends message to everyone logged in

#!/bin/bash

wall <<zzz23EndOfMessagezzz23
Dees ees a message frrom Central Headquarters:
Do not keel moose!
# Other message text goes here.
# Note: Comment lines printed by 'wall'.
zzz23EndOfMessagezzz23

# Could have been done more efficiently by
# wall <message-file

exit 0

Example 3-77. Multi-line message using cat

#!/bin/bash

# 'echo' is fine for printing single line messages,
#  but somewhat problematic for for message blocks.
#  A 'cat' here document overcomes this limitation.

cat <<End-of-message
-------------------------------------
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
This is the last line of the message.
-------------------------------------
End-of-message

exit 0

Example 3-78. upload: Uploads a file pair to "Sunsite" incoming directory

#!/bin/bash

# upload
# upload file pair (filename.lsm, filename.tar.gz)
# to incoming directory at Sunsite


if [ -z $1 ]
then
  echo "Usage: `basename $0` filename"
  exit 1
fi  


Filename=`basename $1`
# Strips pathname out of file name

Server="metalab.unc.edu"
Directory="/incoming/Linux"
# These need not be hard-coded into script,
# may instead be changed to command line argument.

Password="your.e-mail.address"
# Change above to suit.

ftp -n $Server <<End-Of-Session
# -n option disables auto-logon

user anonymous $Password
binary
bell
# Ring 'bell' after each file transfer
cd $Directory
put $Filename.lsm
put $Filename.tar.gz
bye
End-Of-Session

exit 0

Note: Some utilities will not work in a "here document". The pagers, more and less are among these.

For those tasks too complex for a "here document", consider using the expect scripting language, which is specifically tailored for feeding input into non-interactive programs.




Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру