zsh-users
 help / color / mirror / code / Atom feed
From: William Park <opengeometry@yahoo.ca>
To: zsh-users@sunsite.dk
Subject: Re: (feature request) Shell script within shell script
Date: Sat, 1 Feb 2003 12:04:08 -0500	[thread overview]
Message-ID: <20030201170408.GA4678@node1.opengeometry.net> (raw)
In-Reply-To: <BBEKIEKOHGEFJBFLEENGEEFECGAA.piyokun@email.com>

On Sun, Feb 02, 2003 at 12:48:01AM +0900, Clifford Caoile wrote:
> William Park:
> 
> I am not an expert, but I was exploring here documents in bash and zsh
> just this last week. Let me see if I get this right:
> 
> In zsh-4.0.x, you can use "here documents" or "here-strings". A here
> document has this syntax (note there is no double quotations
> necessary):
> 
> >> /etc/hosts <<EOF
> 192.168.0.26 mynewcomputer.zsh.org
> EOF
> 
> This adds the line to the /etc/hosts file. The cat command is
> implicit.  (It's still a useless cat trick, right?)
> 
> A "here string" uses the <<< construct, but I have not used this.
> Please check the docs.
> 
> You can also strip indentation by typing "<<-" instead of just "<<".
> 
> There is also interpretation done within the here document. This has
> bitten me a couple of times. Remember in a bash or zsh here document
> that the '$' (dollar sign), '\' (backslash) and the '`' (backquote)
> are interpreted even inside a here document, so you must prepend a
> backslash, i.e. '\$' and '\\' and '\`'. This make the here document
> look gastly horrid. You mention you will be using Awk/Python/Perl/etc.
> I know that Perl uses $ a lot. Also Awk?  What you will be typing will
> not be easy to keep track because zsh silently substitutes text.
> Debugging will be hard. Is it possible to turn this off?

If you quote EOF (ie. "EOF"), then the content of Here Documents will
not be expanded.  So, no problem with '$', '\', and '`'.

> 
> Of course, you could always try to construct your foreign language
> script line by line. That is what I did before, like this:
> 
> rm $tmpcmdfile
> echo '#! /usr/bin/perl' >> $tmpcmdfile
> ...
> chmod 700 $tmpcmdfile
> $tmpcmdfile
> rm $tmpcmdfile
> 
> Do you like typing? ;-) This example actually led me to exploring here
> documents.
> 
> Specifically with your example, I am unable to ascertain what you want
> to do. Do you want to execute that here document on the line where you
> declare it? What does that "herefile" mean? Is that syntactic sugar
> meaning "foreign language eval?" In any event, I do not know how to do
> that, and I don't think that's a zsh-4.0.x capability.
> 
> Can a real expert answer please? ;-)

However, I'm not talking about redirecting into a real file.  You can do
that with Here Document easily enough.  What I want is similiar to
function.  You define it in the main shell script, and shell will read
and store it in memory.  When you call it, shell will execute the
content just as though it is a real external file script.

If the here content is short, then I can do something like
	awk '...'
or
	python '
	...	(due to indentation)
	'
instead of Here Document.  Of course, the 2 levels of quotes are pain.
The convenience is that you only have to edit 1 file.


> | When writing a shell script, quite often, I have to write bunch of
> | separate Awk/Python/Perl/etc scripts that are more than few lines.  I
> | tend to lose track of which script does what, and get bitten over two
> | levels of quotes.
> |
> | Essentially, I wish I could do something like
> |
> |     herefile test1 << "EOF"
> |     #! /usr/bin/gawk -f
> |     ...
> |     ...
> |     EOF
> |
> | and
> |
> |     herefile test2 << "EOF"
> |     #! /usr/bin/python
> |     ...
> |     ...
> |     EOF
> |
> | so that invocation and usage of 'test1' and 'test2' will be the same as
> | if they were separate real files.
> |
> | Can Zsh do this now?  If not, does Zsh have standard mechanism to
> | provide this kind of extensions?

-- 
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data management and processing. 


  parent reply	other threads:[~2003-02-01 17:04 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20030128042243.GA3888@node1.opengeometry.net>
     [not found] ` <20030128104034.GA6470@node1.opengeometry.net>
     [not found]   ` <20030131204945.GA1189@node1.opengeometry.net>
2003-02-01  7:36     ` William Park
2003-02-01 15:48       ` Clifford Caoile
2003-02-01 16:56         ` Will Yardley
2003-02-01 17:58           ` Clifford Caoile
2003-02-01 17:04         ` William Park [this message]
2003-02-01 19:29       ` Bart Schaefer
2003-02-03 10:31         ` Peter Stephenson
2003-02-03 23:15       ` William Park
2003-02-04  9:18         ` Bart Schaefer
2003-02-04  9:28           ` William Park
2003-02-04 17:24             ` Bart Schaefer
2003-02-04 17:39               ` William Park
2003-02-07 20:23           ` William Park
2003-02-08  2:31             ` Bart Schaefer
2003-02-08 19:37               ` William Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030201170408.GA4678@node1.opengeometry.net \
    --to=opengeometry@yahoo.ca \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).