zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: (feature request) Shell script within shell script
Date: Sat, 1 Feb 2003 19:29:38 +0000	[thread overview]
Message-ID: <1030201192938.ZM7570@candle.brasslantern.com> (raw)
In-Reply-To: <20030201073655.GA3893@node1.opengeometry.net>

On Feb 1,  2:36am, William Park wrote:
} Subject: Re: (feature request) Shell script within shell script
}
} Essentially, I wish I could do something like
}     
}     herefile test1 << "EOF"
}     #! /usr/bin/gawk -f
}     ...
}     ...
}     EOF

Effectively, what you want to do is create a temp file and then execute
it.

That's as simple as:

    herefile () {
      setopt localtraps noshwordsplit
      local tmp=${TMPPREFIX}HERE$$
      trap "command rm -f $tmp" EXIT
      : >| $tmp				|| return
      chmod u=rwx,go-rwx $tmp		|| return
      cat >| $tmp			|| return
      $tmp $@
    }

Try it with this silly example:

    herefile <<\EOF
    #! /bin/ls -l
    EOF

The tricky bit of course is that the redirection becomes the standard
input of the "herefile" function, so your script will see end-of-file
as soon as it starts running.

You can avoid this by doing some file descriptor manipulations:

    _herefile () {
      setopt localtraps noshwordsplit
      local tmp=${TMPPREFIX}HERE$$
      trap "command rm -f $tmp" EXIT
      : >| $tmp				|| return
      chmod u=rwx,go-rwx $tmp		|| return
      cat >| $tmp			|| return
      $tmp $@ <&3 3<&-
    }
    alias herefile='_herefile 3<&0'

    herefile /etc/HOSTNAME /etc/issue <<\EOF
    #! /usr/bin/perl -p
    BEGIN { print join("\n\t", 'Printing these files: ', @ARGV)."\n\n"; }
    EOF

This of course has the drawback of always consuming file descriptor 3,
so it may not work well with other scripts or functions that do lots
of file-descriptor swapping.  You can pick another descriptor number 
if you think it'll make collisions less likely.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


  parent reply	other threads:[~2003-02-01 19:30 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
2003-02-01 19:29       ` Bart Schaefer [this message]
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=1030201192938.ZM7570@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --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).