Sometimes, when you do shell scripting, you have to call another command that need manual input.
For example, you want to send email from the shell
# mail adiguna@domain.com
To: adiguna@domain.com
From: you@domain.com
Hello!
.
#
You need to type manually the three last lines to send email.
You might ask how to handle this without interaction.
That’s when “here” document comes handy.
You can re-write the script like this
# mail adiguna@domain.com << END
To: adiguna@domain.com
From: you@adiguna@domain.com
Hello!
.
END
The syntax for here-document is like this:
command << KEYWORD
input1
input2
input3
KEYWORD
I usually use this for mail and Sybase query (isql). For example:
isql -Usa -SSYB -Ppassword -w2000<<-! | egrep -v affected > cgi3.txt
use omcdb
go
select convert(char(10),c.imap_name) as BSC,
convert(char(25),b.imap_name) as SiteName,
convert(char(25),a.imap_name) as CellName,
convert(char(20),a.imap_CGI) as CGI
from BSCCell a,
BSCBTSSiteManager b,
BSCNE c where a.fdn like b.fdn+’.%’ and a.fdn like c.fdn+’.%’ and b.fdn like c.fdn+’.%’
order by c.imap_name
go
quit
!
Or you can also automate your ftp like this :
ftp -in noc4web << end
user anonymous email
prompt off
ascii
mput *.*
bye
end
January 16, 2007 at 9:49 pm
The two characters `-!’ look confusing. Is there a reason why you choose those instead of a simple `END’?
January 18, 2007 at 8:49 pm
The ‘-!’ suppresses leading tabs (but not spaces) in the output, might be useful to make script more readable. Well, I copied that script from other script that contains html formatting :).
May 19, 2014 at 1:37 am
Hey there, You have done an incredible job. I will definitely digg it
and personally recommend to my friends. I’m confident they’ll be
benefited from this web site.