vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| simple question... cat << EOF foo bar EOF echos foo and bar... The question is - is that the shell (bash) or ??? that supports that function? Simple question I'm sure, but googling for << doesn't work.... Either zsh supports that as well or it's not bash... I'm confused. Ray |
| |||
| -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 ray wrote: > simple question... > > cat << EOF > foo > bar > EOF > > echos foo and bar... > > The question is - is that the shell (bash) or ??? that supports that > function? > > Simple question I'm sure, but googling for << doesn't work.... > > > Either zsh supports that as well or it's not bash... I'm confused. It's called a "here document". Google for that term, and you'll get a much better explanation than I can give. But, in brief it is a feature of the shell; bash supports it, and (apparently) so does zsh. The construct <<something data data something tells the shell to collect all the lines following the <<something, up to but not including the trailing 'something' and feed that data into the processes stdin. Shell variable expansion will be performed, given the suitable set up of your shell. So, for instance, DATE=`date` cat >/etc/issue.net <<EOF Hello there. This file was built on $DATE You have reached my system. Nice, isnt it? EOF will expand the $DATE field, and copy the resulting text into your /etc/issue.net file See bash(1) for details You might also read http://www.faqs.org/docs/abs/HTML/here-docs.html - -- Lew Pitcher, IT Specialist, Enterprise Data Systems Enterprise Technology Solutions, TD Bank Financial Group (Opinions expressed here are my own, not my employer's) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) iD8DBQFCpcxBagVFX4UWr64RAtZOAKDaUuMJ0VlaDKv0blEIHx zAUeKp5wCfciyr t5H9+178SZVGPkQggFx/gFw= =gc6C -----END PGP SIGNATURE----- |
| |||
| Lew Pitcher wrote: > It's called a "here document". Google for that term, and you'll get a > much better explanation than I can give. > > See bash(1) for details > You might also read http://www.faqs.org/docs/abs/HTML/here-docs.html > awesome, thanks! I've used it in perl, but didn't know bash supported it (until recently) and didn't know the name of it. I'm a windows batch file guy at work... moving to perl and bash has been both enlighting and confusing and wonderful and overwhelming at the same time. (cygwin and perl are starting to show up in my scripts at work...) Ray |
| |||
| On Tue, 07 Jun 2005 12:33:07 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote: > > So, for instance, > DATE=`date` > cat >/etc/issue.net <<EOF > Hello there. This file was built on $DATE > You have reached my system. Nice, isnt it? > EOF > > will expand the $DATE field, and copy the resulting text into your > /etc/issue.net file Did you actualy _try_ that? Indented EOF needs a '-' elsewhere I'll let you find elsewhere... --Grant. |
| |||
| -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Grant Coady wrote: > On Tue, 07 Jun 2005 12:33:07 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote: > >>So, for instance, >> DATE=`date` >> cat >/etc/issue.net <<EOF >> Hello there. This file was built on $DATE >> You have reached my system. Nice, isnt it? >> EOF >> >>will expand the $DATE field, and copy the resulting text into your >>/etc/issue.net file > > > Did you actualy _try_ that? Indented EOF needs a '-' elsewhere > > I'll let you find elsewhere... > > --Grant. > FWIW, in usenet posts and emails, I typically indent code examples by two characters to highlight them /as code examples/. The indentation (in the post) isn't meant to be copied literally. As for the '-', then DATE=`date` cat >/etc/issue.net <<-EOF Hello there. This file was built on $DATE You have reached my system. Nice, isnt it? EOF To quote bash(1) "If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line con- taining delimiter. This allows here-documents within shell scripts to be indented in a natural fashion." - -- Lew Pitcher, IT Specialist, Enterprise Data Systems Enterprise Technology Solutions, TD Bank Financial Group (Opinions expressed here are my own, not my employer's) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) iD8DBQFCpfuyagVFX4UWr64RAhhoAJ4uMX8LY3OiinNzSKtIXO 1mYFAbegCcDg5d bSG3zEBrQmoKmLG7h9mTvn8= =5Ge8 -----END PGP SIGNATURE----- |
| |||
| ray <nospam@example.com> wrote: > The question is - is that the shell (bash) or ??? that supports that > function? It is actually a generic shell function, any self-respecting shell should support it (and that includes bash, tcsh, ksh and even the original Bourne shell: <<[-]word The shell input is read up to a line that is the same as word, or to an end-of-file. The resulting document becomes the standard input. If any character of word is quoted, no interpretation is placed upon the characters of the document; otherwise, parameter and command substitution occurs, (unescaped) \new-line is ignored, and \ must be used to quote the characters \, $, `, and the first character of word. If - is appended to <<, all leading tabs are stripped from word and from the document. from the man page for the Bourne shell on a HP-UX - real Unix SystemV - workstation). As people already told you, it's called a "here document", as it was invented to put the input for a command INTO the script itself (the input document is "here"). -- ************************************************** ****************** ** Eef Hartman, Delft University of Technology, dept. EWI/TW ** ** e-mail: E.J.M.Hartman@math.tudelft.nl, fax: +31-15-278 7295 ** ** snail-mail: P.O. Box 5031, 2600 GA Delft, The Netherlands ** ************************************************** ****************** |
| |||
| On 2005-06-07, ray <nospam@example.com> wrote: > simple question... > > cat << EOF > foo > bar > EOF > > echos foo and bar... > > The question is - is that the shell (bash) or ??? that supports that > function? Yes, bash and other scripting languages. Here's some good info: http://linuxcommand.org/wss0030.php nb |
| |||
| On Tue, 07 Jun 2005 15:55:30 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote: > > FWIW, in usenet posts and emails, I typically indent code examples by > two characters to highlight them /as code examples/. The indentation (in > the post) isn't meant to be copied literally. My apologies, sometimes I shouldn't answer posts half-asleep, but it might confuse reader if it not work after copy/paste > > As for the '-', then > DATE=`date` > cat >/etc/issue.net <<-EOF > Hello there. This file was built on $DATE > You have reached my system. Nice, isnt it? > EOF Would've used echo, but then, with so many ways to do things, 'correct' defined as 'does it do the expected?' Not style. I tend to think of 'here' documents as data arrays, similar syntax to reading a file in shell script. Only because that's the sort of utility I been writing lately. Didn't pick on your backticks --Grant. |
| |||
| -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Grant Coady wrote: > On Tue, 07 Jun 2005 15:55:30 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote: > >>FWIW, in usenet posts and emails, I typically indent code examples by >>two characters to highlight them /as code examples/. The indentation (in >>the post) isn't meant to be copied literally. > > > My apologies, sometimes I shouldn't answer posts half-asleep, but it > might confuse reader if it not work after copy/paste No problem. No offence meant and none taken. I hadn't thought of the cut'n'paste angle. Perhaps I had better put sentinals around code examples to facilitate that sort of thing. > >>As for the '-', then >> DATE=`date` >> cat >/etc/issue.net <<-EOF >> Hello there. This file was built on $DATE >> You have reached my system. Nice, isnt it? >> EOF > > > Would've used echo, but then, with so many ways to do things, > 'correct' defined as 'does it do the expected?' Not style. > I tend to think of 'here' documents as data arrays, similar > syntax to reading a file in shell script. Only because that's > the sort of utility I been writing lately. Didn't pick on > your backticks Again, not a problem. I was trying for a /simple/ example to show that shell variable substitution was handled in the here document. P'haps I didn't pick as transparent an example as I should have. I should have pointed the OP at an article I wrote for The Linux Gazette that had a good example of the use of a here document. In http://linuxgazette.net/105/pitcher1.html I have a piece of shell script that updates my ISP's webserver with my homepage. The script invokes FTP with a here document (crude, but it does what I wanted) to transfer the built html page to my ISP's server. The FTP script looks like #-------------code example---------- #<--- left margin ------ TEMPFILE=`/usr/bin/mktemp /tmp/$1=XXXXXX` || exit 2 # ... elided logic to build html homepage into $TEMPFILE ISP_ADDRESS=webserver.isp.com ISP_USERID=username ISP_PASSWD=password /bin/ftp -n <<STOP open $ISP_ADDRESS user $ISP_USERID $ISP_PASSWD ascii put $TEMPFILE index.htm bye STOP #<--- left margin ------ #-------------code example---------- > --Grant. - -- Lew Pitcher Master Codewright & JOAT-in-training | GPG public key available on request Registered Linux User #112576 (http://counter.li.org/) Slackware - Because I know what I'm doing. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFCpjHuagVFX4UWr64RApFnAJwIely6puvc/wu+Ta/73Sd6Y5hYbACgmkRL oFJxT5YC1DgUdWs7W0xv/Ss= =biBp -----END PGP SIGNATURE----- |
| ||||
| Lew Pitcher <Lew.Pitcher@td.com> trolled: pgp trash troll delete > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > ray wrote: >> simple question... >> >> cat << EOF >> foo >> bar >> EOF >> >> echos foo and bar... >> >> The question is - is that the shell (bash) or ??? that supports that >> function? >> >> Simple question I'm sure, but googling for << doesn't work.... >> >> >> Either zsh supports that as well or it's not bash... I'm confused. > > It's called a "here document". Google for that term, and you'll get a > much better explanation than I can give. > > But, in brief > it is a feature of the shell; bash supports it, and (apparently) so does > zsh. > > The construct > <<something > data > data > something > > tells the shell to collect all the lines following the <<something, up > to but not including the trailing 'something' and feed that data into > the processes stdin. > > Shell variable expansion will be performed, given the suitable set up of > your shell. > > So, for instance, > DATE=`date` > cat >/etc/issue.net <<EOF > Hello there. This file was built on $DATE > You have reached my system. Nice, isnt it? > EOF > > will expand the $DATE field, and copy the resulting text into your > /etc/issue.net file > > See bash(1) for details > You might also read http://www.faqs.org/docs/abs/HTML/here-docs.html > > > > > - -- > > Lew Pitcher, IT Specialist, Enterprise Data Systems > Enterprise Technology Solutions, TD Bank Financial Group > > (Opinions expressed here are my own, not my employer's) > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.4 (MingW32) > > iD8DBQFCpcxBagVFX4UWr64RAtZOAKDaUuMJ0VlaDKv0blEIHx zAUeKp5wCfciyr > t5H9+178SZVGPkQggFx/gFw= > =gc6C > -----END PGP SIGNATURE----- _________________________________________ Usenet Zone Free Binaries Usenet Server More than 120,000 groups Unlimited download http://www.usenetzone.com to open account |