help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] shebang for language that does not use # for comment


From: Eric Blake
Subject: Re: [Help-bash] shebang for language that does not use # for comment
Date: Tue, 03 Jul 2012 19:44:54 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1

On 07/03/2012 06:14 PM, Peng Yu wrote:
>> and if you don't like putting the EOF at the end, you could use hacks like:
>> ====
>> #!/bin/bash
>> shopt -s lastpipe; tail -n +2 "$0" | exec /usr/bin/env gp

An alternative to using lastpipe (and with your correction of tail -n+3)
would be:

tail -n +3 "$0" | /usr/bin/env gp; exit

But note that the alternative means that bash sticks around for the
entire duration of the gp child process, in order to process its exit
status.  It would be nice to reuse the bash process, via exec, for one
less process during the duration of the rest of the script, but you have
a problem that normally the use of a pipe (|) means that bash creates a
subshell.  Using exec in a subshell means that gp reuses the subshell
process, but the parent process is still around.  That is, this won't work:

tail -n +3 "$0" | exec /usr/bin/env gp

unless there is a way to force the 'exec' to occur in the current shell
instead of a subshell.  In fact, that's precisely what the lastpipe
shell option was invented for.

Another alternative to lastpipe (since it was not present in older bash)
would be avoiding the subshell due to | altogether, by using a different
extension such as <():

exec /usr/bin/env gp < <(tail -n +3 "$0")

> 
> BTW, how "job control" can be nonactive?

set +m

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org



Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]