* zsh portable script
@ 2010-07-12 14:46 Atom Smasher
2010-07-12 15:35 ` Sebastian Stark
` (3 more replies)
0 siblings, 4 replies; 29+ messages in thread
From: Atom Smasher @ 2010-07-12 14:46 UTC (permalink / raw)
To: zsh-users
on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most other
systems?) it installs as /usr/bin/zsh.
what's the best way to make zsh script portable between linux and freebsd?
i could start the script with:
#!/usr/bin/env zsh
or i could specify that the script be executed as:
zsh script
is there a better way?
--
...atom
________________________
http://atom.smasher.org/
762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
-------------------------------------------------
"WAR IS PEACE, FREEDOM IS SLAVERY, IGNORANCE IS STRENGTH"
The two minutes hate ended with this message
which is the slogan of the Party.
-- George Orwell
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 14:46 zsh portable script Atom Smasher
@ 2010-07-12 15:35 ` Sebastian Stark
2010-07-12 16:10 ` Joke de Buhr
2010-07-12 16:17 ` Vincent Lefevre
2010-07-12 15:37 ` Joke de Buhr
` (2 subsequent siblings)
3 siblings, 2 replies; 29+ messages in thread
From: Sebastian Stark @ 2010-07-12 15:35 UTC (permalink / raw)
To: Atom Smasher; +Cc: zsh-users
Am 12.07.2010 um 16:46 schrieb Atom Smasher:
> on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most other systems?) it installs as /usr/bin/zsh.
>
> what's the best way to make zsh script portable between linux and freebsd?
>
> i could start the script with:
> #!/usr/bin/env zsh
>
> or i could specify that the script be executed as:
> zsh script
>
> is there a better way?
/usr/bin/env is not as portable as one might think (see http://www.in-ulm.de/~mascheck/various/shebang/#env)
When I read this I was thinking about it a bit and came to this:
------------------------------------------------
#!/bin/sh
if test -z "$ZSH_VERSION"
then
exec zsh <"$0"
fi
print "I'm in zsh now!"
------------------------------------------------
Since the script is fed to /bin/sh via stdin it shouldn't be a problem if it contains zsh-specific syntax after the exec. I never used this construct and I'm not sure how portable it really is. Also I don't know if one can assume that /bin/sh understands ,exec' and ,<'.
Sebastian
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 14:46 zsh portable script Atom Smasher
2010-07-12 15:35 ` Sebastian Stark
@ 2010-07-12 15:37 ` Joke de Buhr
2010-07-12 15:45 ` Joke de Buhr
` (2 more replies)
2010-07-12 15:37 ` Vincent Lefevre
2010-07-12 16:06 ` Peter Stephenson
3 siblings, 3 replies; 29+ messages in thread
From: Joke de Buhr @ 2010-07-12 15:37 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: Text/Plain, Size: 1045 bytes --]
On Monday 12 July 2010 16:46:22 Atom Smasher wrote:
> on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most other
> systems?) it installs as /usr/bin/zsh.
>
> what's the best way to make zsh script portable between linux and freebsd?
>
> i could start the script with:
> #!/usr/bin/env zsh
>
> or i could specify that the script be executed as:
> zsh script
>
> is there a better way?
Using env doesn't solve the problem either. There is no guarantee the "env"
program is installed under /usr/bin/env. It may as well be installed under
/usr/local/bin/env.
But every unix should have a bourne shell compatible shell located at /bin/sh.
You can use a bourne shell compatible code snippet and call zsh from it. If
PATH contains zsh than this will do the trick.
#!/bin/sh
if [ -z "$ZSH_VERSION" ]; then
## searching PATH for zsh executable
exec zsh $* < $0
## no zsh detected
exit 1
fi
## zsh should be running now. do zsh stuff
print "hello"
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 706 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 14:46 zsh portable script Atom Smasher
2010-07-12 15:35 ` Sebastian Stark
2010-07-12 15:37 ` Joke de Buhr
@ 2010-07-12 15:37 ` Vincent Lefevre
2010-07-12 16:06 ` Peter Stephenson
3 siblings, 0 replies; 29+ messages in thread
From: Vincent Lefevre @ 2010-07-12 15:37 UTC (permalink / raw)
To: zsh-users
On 2010-07-13 02:46:22 +1200, Atom Smasher wrote:
> i could start the script with:
> #!/usr/bin/env zsh
This is what I've always done. But note that it is not possible to
give arguments (options) in a portable way. However, with zsh, this
is not really useful.
Alternatively, you can start your script with something like:
#!/bin/sh
[ -n "$ZSH_VERSION" ] || exec zsh -- "$0" ${1+"$@"}
(not tested).
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 15:37 ` Joke de Buhr
@ 2010-07-12 15:45 ` Joke de Buhr
2010-07-12 16:01 ` Frank Terbeck
2010-07-12 16:15 ` Vincent Lefevre
2010-07-13 13:43 ` François Revol
2010-07-20 13:53 ` Thorsten Kampe
2 siblings, 2 replies; 29+ messages in thread
From: Joke de Buhr @ 2010-07-12 15:45 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: Text/Plain, Size: 1439 bytes --]
On Monday 12 July 2010 17:37:19 Joke de Buhr wrote:
> On Monday 12 July 2010 16:46:22 Atom Smasher wrote:
> > on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most other
> > systems?) it installs as /usr/bin/zsh.
> >
> > what's the best way to make zsh script portable between linux and
> > freebsd?
> >
> > i could start the script with:
> > #!/usr/bin/env zsh
> >
> > or i could specify that the script be executed as:
> > zsh script
> >
> > is there a better way?
>
> Using env doesn't solve the problem either. There is no guarantee the "env"
> program is installed under /usr/bin/env. It may as well be installed under
> /usr/local/bin/env.
>
> But every unix should have a bourne shell compatible shell located at
> /bin/sh. You can use a bourne shell compatible code snippet and call zsh
> from it. If PATH contains zsh than this will do the trick.
>
>
> #!/bin/sh
>
> if [ -z "$ZSH_VERSION" ]; then
> ## searching PATH for zsh executable
> exec zsh $* < $0
>
> ## no zsh detected
> exit 1
> fi
>
> ## zsh should be running now. do zsh stuff
> print "hello"
sorry, the correct version should be:
#!/bin/sh
if [ -z "$ZSH_VERSION" ]; then
## searching PATH for zsh executable
exec zsh $0 $*
## no zsh detected
exit 1
fi
## zsh should be running now. do zsh stuff
print "hello"
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 706 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 15:45 ` Joke de Buhr
@ 2010-07-12 16:01 ` Frank Terbeck
2010-07-12 16:15 ` Vincent Lefevre
1 sibling, 0 replies; 29+ messages in thread
From: Frank Terbeck @ 2010-07-12 16:01 UTC (permalink / raw)
To: Joke de Buhr; +Cc: zsh-users
[-- Attachment #1: Type: text/plain, Size: 608 bytes --]
Joke de Buhr wrote:
[...]
> #!/bin/sh
>=20=20=20
> if [ -z "$ZSH_VERSION" ]; then
> ## searching PATH for zsh executable
> exec zsh $0 $*
That should rather be
exec zsh "$0" "$@"
Or even better, as Vicent put it:
exec zsh -- "$0" ${1+"$@"}
The `--' should be pretty clear. About the ${1+"$@"}, see:
<http://www.in-ulm.de/~mascheck/various/bourne_args/>
Regards, Frank
=2D-=20
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
-- RFC 1925
[-- Attachment #2: Type: application/pgp-signature, Size: 196 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 14:46 zsh portable script Atom Smasher
` (2 preceding siblings ...)
2010-07-12 15:37 ` Vincent Lefevre
@ 2010-07-12 16:06 ` Peter Stephenson
3 siblings, 0 replies; 29+ messages in thread
From: Peter Stephenson @ 2010-07-12 16:06 UTC (permalink / raw)
To: zsh-users
On Tue, 13 Jul 2010 02:46:22 +1200 (NZST)
Atom Smasher <atom@smasher.org> wrote:
> on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most
> other systems?) it installs as /usr/bin/zsh.
>
> what's the best way to make zsh script portable between linux and
> freebsd?
>
> i could start the script with:
> #!/usr/bin/env zsh
>
> or i could specify that the script be executed as:
> zsh script
>
> is there a better way?
Looking at the responses, on which I can't improve, one might speculate it
was time for someone to implement path searching down in the #! handler for
relative paths.
#!zsh
print "Found zsh in $0".
I don't think it has any misfeatures that the env method doesn't have, and
it improves on it in important ways.
This doesn't really touch zsh except that we could do it in our own
#! handler (see zexecve() in Src/exec.c).
(Follow-ups should probably go to zsh-workers).
--
Peter Stephenson <pws@csr.com> Software Engineer
Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK
Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 15:35 ` Sebastian Stark
@ 2010-07-12 16:10 ` Joke de Buhr
2010-07-12 16:17 ` Vincent Lefevre
1 sibling, 0 replies; 29+ messages in thread
From: Joke de Buhr @ 2010-07-12 16:10 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: Text/Plain, Size: 1507 bytes --]
On Monday 12 July 2010 17:35:34 Sebastian Stark wrote:
> Am 12.07.2010 um 16:46 schrieb Atom Smasher:
> > on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most other
> > systems?) it installs as /usr/bin/zsh.
> >
> > what's the best way to make zsh script portable between linux and
> > freebsd?
> >
> > i could start the script with:
> > #!/usr/bin/env zsh
> >
> > or i could specify that the script be executed as:
> > zsh script
> >
> > is there a better way?
>
> /usr/bin/env is not as portable as one might think (see
> http://www.in-ulm.de/~mascheck/various/shebang/#env)
>
> When I read this I was thinking about it a bit and came to this:
>
> ------------------------------------------------
> #!/bin/sh
>
> if test -z "$ZSH_VERSION"
> then
> exec zsh <"$0"
> fi
>
> print "I'm in zsh now!"
> ------------------------------------------------
>
> Since the script is fed to /bin/sh via stdin it shouldn't be a problem if
> it contains zsh-specific syntax after the exec. I never used this
> construct and I'm not sure how portable it really is. Also I don't know if
> one can assume that /bin/sh understands ,exec' and ,<'.
>
>
> Sebastian
Sorry I wrote I the version with the "<" in the first place.
calling zsh this way doesn't pass invocation arguments. If that is not
necessary everything should be find.
/bin/sh needs to be a bourne compatible shell and therefore must understand
"exec" and "<"-redirection.
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 706 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 15:45 ` Joke de Buhr
2010-07-12 16:01 ` Frank Terbeck
@ 2010-07-12 16:15 ` Vincent Lefevre
2010-07-12 16:18 ` Vincent Lefevre
` (2 more replies)
1 sibling, 3 replies; 29+ messages in thread
From: Vincent Lefevre @ 2010-07-12 16:15 UTC (permalink / raw)
To: zsh-users
On 2010-07-12 17:45:18 +0200, Joke de Buhr wrote:
> sorry, the correct version should be:
>
> #!/bin/sh
>
> if [ -z "$ZSH_VERSION" ]; then
> ## searching PATH for zsh executable
> exec zsh $0 $*
As arguments may contain spaces, this is incorrect. This should be:
exec zsh "$0" ${1+"$@"}
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 15:35 ` Sebastian Stark
2010-07-12 16:10 ` Joke de Buhr
@ 2010-07-12 16:17 ` Vincent Lefevre
1 sibling, 0 replies; 29+ messages in thread
From: Vincent Lefevre @ 2010-07-12 16:17 UTC (permalink / raw)
To: zsh-users
On 2010-07-12 17:35:34 +0200, Sebastian Stark wrote:
> When I read this I was thinking about it a bit and came to this:
>
> ------------------------------------------------
> #!/bin/sh
>
> if test -z "$ZSH_VERSION"
> then
> exec zsh <"$0"
> fi
This is incorrect. It won't handle the arguments.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 16:15 ` Vincent Lefevre
@ 2010-07-12 16:18 ` Vincent Lefevre
2010-07-12 16:31 ` Joke de Buhr
2010-07-12 16:22 ` Sebastian Stark
2010-07-13 13:02 ` Atom Smasher
2 siblings, 1 reply; 29+ messages in thread
From: Vincent Lefevre @ 2010-07-12 16:18 UTC (permalink / raw)
To: zsh-users
On 2010-07-12 18:15:19 +0200, Vincent Lefevre wrote:
> On 2010-07-12 17:45:18 +0200, Joke de Buhr wrote:
> > sorry, the correct version should be:
> >
> > #!/bin/sh
> >
> > if [ -z "$ZSH_VERSION" ]; then
> > ## searching PATH for zsh executable
> > exec zsh $0 $*
>
> As arguments may contain spaces, this is incorrect. This should be:
>
> exec zsh "$0" ${1+"$@"}
Sorry,
exec zsh -- "$0" ${1+"$@"}
as I said in my previous reply.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 16:15 ` Vincent Lefevre
2010-07-12 16:18 ` Vincent Lefevre
@ 2010-07-12 16:22 ` Sebastian Stark
2010-07-12 16:39 ` Vincent Lefevre
2010-07-13 13:02 ` Atom Smasher
2 siblings, 1 reply; 29+ messages in thread
From: Sebastian Stark @ 2010-07-12 16:22 UTC (permalink / raw)
To: Vincent Lefevre; +Cc: zsh-users
Am 12.07.2010 um 18:15 schrieb Vincent Lefevre:
> On 2010-07-12 17:45:18 +0200, Joke de Buhr wrote:
>> sorry, the correct version should be:
>>
>> #!/bin/sh
>>
>> if [ -z "$ZSH_VERSION" ]; then
>> ## searching PATH for zsh executable
>> exec zsh $0 $*
>
> As arguments may contain spaces, this is incorrect. This should be:
>
> exec zsh "$0" ${1+"$@"}
is this something every POSIX-compatible bourne shell understands?
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 16:18 ` Vincent Lefevre
@ 2010-07-12 16:31 ` Joke de Buhr
2010-07-12 16:43 ` Vincent Lefevre
0 siblings, 1 reply; 29+ messages in thread
From: Joke de Buhr @ 2010-07-12 16:31 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: Text/Plain, Size: 853 bytes --]
On Monday 12 July 2010 18:18:52 Vincent Lefevre wrote:
> On 2010-07-12 18:15:19 +0200, Vincent Lefevre wrote:
> > On 2010-07-12 17:45:18 +0200, Joke de Buhr wrote:
> > > sorry, the correct version should be:
> > > #!/bin/sh
> > >
> > > if [ -z "$ZSH_VERSION" ]; then
> > >
> > > ## searching PATH for zsh executable
> > > exec zsh $0 $*
> >
> > As arguments may contain spaces, this is incorrect. This should be:
> > exec zsh "$0" ${1+"$@"}
>
> Sorry,
>
> exec zsh -- "$0" ${1+"$@"}
>
> as I said in my previous reply.
Just for the fun of it. An executable file without the #! line is correct
either. So just write:
---------------------- file ----------------------
[ -z "$ZSH_VERSION" ] && exec zsh -- "$0" ${1+"$@"}
print "hello from zsh"
--------------------------------------------------
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 706 bytes --]
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 16:22 ` Sebastian Stark
@ 2010-07-12 16:39 ` Vincent Lefevre
2010-07-12 16:47 ` Frank Terbeck
0 siblings, 1 reply; 29+ messages in thread
From: Vincent Lefevre @ 2010-07-12 16:39 UTC (permalink / raw)
To: zsh-users
On 2010-07-12 18:22:22 +0200, Sebastian Stark wrote:
> Am 12.07.2010 um 18:15 schrieb Vincent Lefevre:
> > exec zsh "$0" ${1+"$@"}
exec zsh -- "$0" ${1+"$@"}
> is this something every POSIX-compatible bourne shell understands?
AFAIK, this works with both POSIX shells and traditional Bourne shells
(at least with the Bourne shell /bin/sh from Solaris).
Note that "$@" alone is not portable, as if there are no arguments,
some shells replace it by "" (any reference?). Hence the ${1+"$@"}.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 16:31 ` Joke de Buhr
@ 2010-07-12 16:43 ` Vincent Lefevre
0 siblings, 0 replies; 29+ messages in thread
From: Vincent Lefevre @ 2010-07-12 16:43 UTC (permalink / raw)
To: zsh-users
On 2010-07-12 18:31:57 +0200, Joke de Buhr wrote:
> Just for the fun of it. An executable file without the #! line is correct
> either. So just write:
>
> ---------------------- file ----------------------
> [ -z "$ZSH_VERSION" ] && exec zsh -- "$0" ${1+"$@"}
> print "hello from zsh"
> --------------------------------------------------
Yes, however, in practice, providing a #! line should be better so
that editors can recognize the file as a shell script (there are
other ways, but probably less portable).
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 16:39 ` Vincent Lefevre
@ 2010-07-12 16:47 ` Frank Terbeck
0 siblings, 0 replies; 29+ messages in thread
From: Frank Terbeck @ 2010-07-12 16:47 UTC (permalink / raw)
To: zsh-users
Vincent Lefevre wrote:
> Note that "$@" alone is not portable, as if there are no arguments,
> some shells replace it by "" (any reference?). Hence the ${1+"$@"}.
<http://www.in-ulm.de/~mascheck/various/bourne_args/>
Regards, Frank
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 16:15 ` Vincent Lefevre
2010-07-12 16:18 ` Vincent Lefevre
2010-07-12 16:22 ` Sebastian Stark
@ 2010-07-13 13:02 ` Atom Smasher
2010-07-13 14:29 ` Benjamin R. Haskell
2 siblings, 1 reply; 29+ messages in thread
From: Atom Smasher @ 2010-07-13 13:02 UTC (permalink / raw)
To: zsh-users
as i suspected, there isn't a *good* way to do it.
instructions at the top of the script suggest putting it in a crontab as:
* * * * * /path/to/zsh /path/to/script
btw, this is the bat-mon script that, until recently, only worked with
freebsd. now it also works with linux.
http://smasher.org/zsh/
enjoy...
--
...atom
________________________
http://atom.smasher.org/
762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
-------------------------------------------------
"To announce that there must be no criticism of the
president, or that we are to stand by the president,
right or wrong, is not only unpatriotic and servile,
but morally treasonable to the American public."
-- Theodore Roosevelt
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 15:37 ` Joke de Buhr
2010-07-12 15:45 ` Joke de Buhr
@ 2010-07-13 13:43 ` François Revol
2010-07-20 14:01 ` Thorsten Kampe
2010-07-20 13:53 ` Thorsten Kampe
2 siblings, 1 reply; 29+ messages in thread
From: François Revol @ 2010-07-13 13:43 UTC (permalink / raw)
To: zsh-users
Le 12 juil. 2010 à 17:37, Joke de Buhr a écrit :
> On Monday 12 July 2010 16:46:22 Atom Smasher wrote:
>> on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most other
>> systems?) it installs as /usr/bin/zsh.
>>
>> what's the best way to make zsh script portable between linux and freebsd?
>>
>> i could start the script with:
>> #!/usr/bin/env zsh
>>
>> or i could specify that the script be executed as:
>> zsh script
>>
>> is there a better way?
>
> Using env doesn't solve the problem either. There is no guarantee the "env"
> program is installed under /usr/bin/env. It may as well be installed under
> /usr/local/bin/env.
Indeed, this kind of "too smart" stuff fails miserably on BeOS and Haiku, which does not have /usr.
François.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-13 13:02 ` Atom Smasher
@ 2010-07-13 14:29 ` Benjamin R. Haskell
2010-07-13 15:28 ` Atom Smasher
2010-07-14 11:45 ` Atom Smasher
0 siblings, 2 replies; 29+ messages in thread
From: Benjamin R. Haskell @ 2010-07-13 14:29 UTC (permalink / raw)
To: Atom Smasher; +Cc: zsh-users
On Wed, 14 Jul 2010, Atom Smasher wrote:
> as i suspected, there isn't a *good* way to do it.
>
> instructions at the top of the script suggest putting it in a crontab as:
> * * * * * /path/to/zsh /path/to/script
>
> btw, this is the bat-mon script that, until recently, only worked with
> freebsd. now it also works with linux.
>
> http://smasher.org/zsh/
>
> enjoy...
I've one-lined perl scripts at various points to grab roughly the same
information. My favorite:
perl -lnwe 'if($a){print$a/$_}else{$a=$_}' /sys/class/power_supply/BAT0/charge_{now,full}
But, I don't recall ever needing root to read the battery status. (In
the /proc/acpi subsystem that you're using,
/proc/acpi/battery/BAT{0,1}/{info,state} are chmod 0444 on my laptop and
netbook; same for the /sys/class/ stuff I used above.) And on my
FreeBSD webhost where I have pretty resticted access, I seem to be able
to grab a lot of hardware-related info via sysctl.
Are permissions the reason you suggest running it under cron?
Otherwise, why not just regenerate in precmd()?
--
Best,
Ben
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-13 14:29 ` Benjamin R. Haskell
@ 2010-07-13 15:28 ` Atom Smasher
2010-07-13 17:01 ` Benjamin R. Haskell
2010-07-14 11:45 ` Atom Smasher
1 sibling, 1 reply; 29+ messages in thread
From: Atom Smasher @ 2010-07-13 15:28 UTC (permalink / raw)
To: Benjamin R. Haskell; +Cc: zsh-users
On Tue, 13 Jul 2010, Benjamin R. Haskell wrote:
> But, I don't recall ever needing root to read the battery status. (In
> the /proc/acpi subsystem that you're using,
> /proc/acpi/battery/BAT{0,1}/{info,state} are chmod 0444 on my laptop and
> netbook; same for the /sys/class/ stuff I used above.) And on my
> FreeBSD webhost where I have pretty resticted access, I seem to be able
> to grab a lot of hardware-related info via sysctl.
==============
from what i've seen, linux permissions to anything in /proc depend on the
distro, but the same *info* is available to anyone with local access on a
bsd box via sysctl.
i'm more familiar with freebsd than linux. are you saying that
/sys/class/power_supply/BAT0/charge_* will be readable when
/proc/acpi/battery/BAT0/* isn't? if that's the case, i'll update the
script.
> Are permissions the reason you suggest running it under cron? Otherwise,
> why not just regenerate in precmd()?
==================
on my bsd laptop i always have at least one mrxvt with ten shells open. it
seems to make sense to just have root get the info once per minute and let
the shells read it from a file, than have a bunch of shells all invoking
sysctl and all figuring out about the battery.
i suppose it can be done without privileges, and it shouldn't burden the
machine; then i suppose it might make sense to get rid of the battery
script and put the logic into precmd. i suppose the worst case is a
hundred or so shells all working it out for themselves... that shouldn't
cause any problems... except if the machine is bogged down and it takes
that much longer to wait for a prompt that's hung while something it needs
to generate a battery notice is blocked (although that risk is probably
there already with the load monitor). i might be able to add a short
timeout, in which case it will just skip it... something to think about, i
guess.
--
...atom
________________________
http://atom.smasher.org/
762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
-------------------------------------------------
Quidquid latine dictum sit, altum viditur.
(Whatever is said in Latin sounds profound.)
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-13 15:28 ` Atom Smasher
@ 2010-07-13 17:01 ` Benjamin R. Haskell
2010-07-14 1:11 ` Atom Smasher
0 siblings, 1 reply; 29+ messages in thread
From: Benjamin R. Haskell @ 2010-07-13 17:01 UTC (permalink / raw)
To: Atom Smasher; +Cc: zsh-users
On Wed, 14 Jul 2010, Atom Smasher wrote:
> On Tue, 13 Jul 2010, Benjamin R. Haskell wrote:
>
> > But, I don't recall ever needing root to read the battery status.
> > (In the /proc/acpi subsystem that you're using,
> > /proc/acpi/battery/BAT{0,1}/{info,state} are chmod 0444 on my laptop
> > and netbook; same for the /sys/class/ stuff I used above.) And on
> > my FreeBSD webhost where I have pretty resticted access, I seem to
> > be able to grab a lot of hardware-related info via sysctl.
> ==============
>
> from what i've seen, linux permissions to anything in /proc depend on
> the distro, but the same *info* is available to anyone with local
> access on a bsd box via sysctl.
>
> i'm more familiar with freebsd than linux. are you saying that
> /sys/class/power_supply/BAT0/charge_* will be readable when
> /proc/acpi/battery/BAT0/* isn't? if that's the case, i'll update the
> script.
No. I'm saying that I've never seen either one of those without full
read permissions. I've not tried them in multiple distros, but I'd be
surprised if the commonly-used battery monitor 'widgets' required
permissions beyond being a local account.
> > Are permissions the reason you suggest running it under cron?
> > Otherwise, why not just regenerate in precmd()?
> ==================
>
> on my bsd laptop i always have at least one mrxvt with ten shells
> open. it seems to make sense to just have root get the info once per
> minute and let the shells read it from a file, than have a bunch of
> shells all invoking sysctl and all figuring out about the battery.
Are those many shells all frequently generating prompts? precmd only
runs once per prompt.
> i suppose it can be done without privileges, and it shouldn't burden
> the machine; then i suppose it might make sense to get rid of the
> battery script and put the logic into precmd. i suppose the worst case
> is a hundred or so shells all working it out for themselves... that
> shouldn't cause any problems... except if the machine is bogged down
> and it takes that much longer to wait for a prompt that's hung while
> something it needs to generate a battery notice is blocked (although
> that risk is probably there already with the load monitor). i might be
> able to add a short timeout, in which case it will just skip it...
> something to think about, i guess.
Just as a quick benchmark, running my perl one-liner that I posted
before in a tight loop 10000 times only takes ~30 seconds. But that's
including the overhead of starting up perl (which takes ~26 seconds on
my machine).
$ date ; for l in {1..10000} ; perl -lnwe 'if($a){print$a/$_}else{$a=$_}' /sys/class/power_supply/BAT0/charge_{now,full} > /dev/null ; date
Tue Jul 13 12:38:29 EDT 2010
Tue Jul 13 12:38:58 EDT 2010
$ date ; for l in {1..10000} ; perl -lwe 'print 1' > /dev/null ; date
Tue Jul 13 12:39:02 EDT 2010
Tue Jul 13 12:39:28 EDT 2010
And, like I said, since it's a once-per-prompt thing, merely having a
bunch of open shells doesn't necessarily increase the load.
Worst-case, the 'installation process' for using this through precmd
(put this wherever it'll get sourced properly) seems better than the
cron version (put this into root's crontab).
--
Best,
Ben
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-13 17:01 ` Benjamin R. Haskell
@ 2010-07-14 1:11 ` Atom Smasher
0 siblings, 0 replies; 29+ messages in thread
From: Atom Smasher @ 2010-07-14 1:11 UTC (permalink / raw)
To: Benjamin R. Haskell; +Cc: zsh-users
On Tue, 13 Jul 2010, Benjamin R. Haskell wrote:
>> on my bsd laptop i always have at least one mrxvt with ten shells open.
>> it seems to make sense to just have root get the info once per minute
>> and let the shells read it from a file, than have a bunch of shells all
>> invoking sysctl and all figuring out about the battery.
>
> Are those many shells all frequently generating prompts? precmd only
> runs once per prompt.
==================
my prompts are generated ~about~ twice per minute, per shell. that keeps
the time and load averages up to date.
they're staggered randomly, so they don't all happen at once.
--
...atom
________________________
http://atom.smasher.org/
762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
-------------------------------------------------
"The only inherent sin in society lies in hurting others
unnecessarily. Hurting yourself in not sinful - just dumb."
-- Robert Heinlein
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-13 14:29 ` Benjamin R. Haskell
2010-07-13 15:28 ` Atom Smasher
@ 2010-07-14 11:45 ` Atom Smasher
2010-07-14 15:00 ` Atom Smasher
1 sibling, 1 reply; 29+ messages in thread
From: Atom Smasher @ 2010-07-14 11:45 UTC (permalink / raw)
To: Benjamin R. Haskell; +Cc: zsh-users
On Tue, 13 Jul 2010, Benjamin R. Haskell wrote:
> Are permissions the reason you suggest running it under cron? Otherwise,
> why not just regenerate in precmd()?
=================
now i remember why i'm running under cron, instead of in precmd...
http://smasher.org/tmp/zsh-bsd-sysctl-slow.png
freebsd's sysctl is way too slow to invoke every time i draw a prompt.
linux's /sys/ and /proc/ are reasonably fast, but i already had a cron job
written for bsd, and very minor changes were needed to make it work with
linux.
i guess i'll leave it alone for now, but if i find a way to get that
battery info from freebsd more quickly i'll think about adding the battery
functionality directly into the zshrc.
--
...atom
________________________
http://atom.smasher.org/
762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
-------------------------------------------------
"In fact a vegan driving a hummer would be contributing
less greenhouse gas carbon emissions than a meat eater
riding a bicycle."
-- Capt Paul Watson, A Very Inconvenient Truth
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-14 11:45 ` Atom Smasher
@ 2010-07-14 15:00 ` Atom Smasher
0 siblings, 0 replies; 29+ messages in thread
From: Atom Smasher @ 2010-07-14 15:00 UTC (permalink / raw)
To: Benjamin R. Haskell; +Cc: zsh-users
On Wed, 14 Jul 2010, Atom Smasher wrote:
> On Tue, 13 Jul 2010, Benjamin R. Haskell wrote:
>
>> Are permissions the reason you suggest running it under cron?
>> Otherwise, why not just regenerate in precmd()?
> =================
>
> now i remember why i'm running under cron, instead of in precmd...
> http://smasher.org/tmp/zsh-bsd-sysctl-slow.png
>
> freebsd's sysctl is way too slow to invoke every time i draw a prompt.
> linux's /sys/ and /proc/ are reasonably fast, but i already had a cron
> job written for bsd, and very minor changes were needed to make it work
> with linux.
======================
it was just pointed out on freebsd-hackers list that this may not be a
freebsd v linux issue... it may be the hardware. eventually i'll have to
reboot this freebsd laptop, and when i do i'll check it out with an ubuntu
CD. if that also runs slow, then the battery script will have to stay
separate from the zshrc... unless some other changes are made...?
--
...atom
________________________
http://atom.smasher.org/
762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
-------------------------------------------------
"Don't fight it son. Confess quickly!
If you hold out too long you could
jeopardize your credit rating."
-- Brazil
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-12 15:37 ` Joke de Buhr
2010-07-12 15:45 ` Joke de Buhr
2010-07-13 13:43 ` François Revol
@ 2010-07-20 13:53 ` Thorsten Kampe
2010-07-20 14:07 ` François Revol
2 siblings, 1 reply; 29+ messages in thread
From: Thorsten Kampe @ 2010-07-20 13:53 UTC (permalink / raw)
To: zsh-users
* Joke de Buhr (Mon, 12 Jul 2010 17:37:19 +0200)
> On Monday 12 July 2010 16:46:22 Atom Smasher wrote:
> > on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most
> > other systems?) it installs as /usr/bin/zsh.
> >
> > what's the best way to make zsh script portable between linux and
> > freebsd?
> >
> > i could start the script with:
> > #!/usr/bin/env zsh
> >
> > or i could specify that the script be executed as:
> > zsh script
> >
> > is there a better way?
>
> Using env doesn't solve the problem either. There is no guarantee the "env"
> program is installed under /usr/bin/env.
It does solve the problem that's why it's used in (shell) scripting[1].
For Python for example it's the official way to specify the interpreter.
I don't think "Atom Smasher" need a "guarantee" - just a confirmation
that it does indeed work.
Thorsten
[1] http://en.wikipedia.org/wiki/Env
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-13 13:43 ` François Revol
@ 2010-07-20 14:01 ` Thorsten Kampe
2010-07-20 14:13 ` François Revol
0 siblings, 1 reply; 29+ messages in thread
From: Thorsten Kampe @ 2010-07-20 14:01 UTC (permalink / raw)
To: zsh-users
* François Revol (Tue, 13 Jul 2010 15:43:31 +0200)
> Le 12 juil. 2010 à 17:37, Joke de Buhr a écrit :
> > On Monday 12 July 2010 16:46:22 Atom Smasher wrote:
> >> on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most other
> >> systems?) it installs as /usr/bin/zsh.
> >>
> >> what's the best way to make zsh script portable between linux and freebsd?
> >>
> >> i could start the script with:
> >> #!/usr/bin/env zsh
> >>
> >> or i could specify that the script be executed as:
> >> zsh script
> >>
> >> is there a better way?
> >
> > Using env doesn't solve the problem either. There is no guarantee the "env"
> > program is installed under /usr/bin/env. It may as well be installed under
> > /usr/local/bin/env.
>
> Indeed, this kind of "too smart" stuff fails miserably on BeOS and Haiku, which does not have /usr.
Now that is a really convincing argument. BeOS was last updated in 2001
and "Haiku" has reached now Alpha stage after nine years. Seriously, on
systems that don't follow the FHS at all, you will have a lot more
serious problems running any kind of script.
Thorsten
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-20 13:53 ` Thorsten Kampe
@ 2010-07-20 14:07 ` François Revol
2010-07-21 8:34 ` Thorsten Kampe
0 siblings, 1 reply; 29+ messages in thread
From: François Revol @ 2010-07-20 14:07 UTC (permalink / raw)
To: zsh-users
Le 20 juil. 2010 à 15:53, Thorsten Kampe a écrit :
> * Joke de Buhr (Mon, 12 Jul 2010 17:37:19 +0200)
>> On Monday 12 July 2010 16:46:22 Atom Smasher wrote:
>>> on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most
>>> other systems?) it installs as /usr/bin/zsh.
>>>
>>> what's the best way to make zsh script portable between linux and
>>> freebsd?
>>>
>>> i could start the script with:
>>> #!/usr/bin/env zsh
>>>
>>> or i could specify that the script be executed as:
>>> zsh script
>>>
>>> is there a better way?
>>
>> Using env doesn't solve the problem either. There is no guarantee the "env"
>> program is installed under /usr/bin/env.
>
> It does solve the problem that's why it's used in (shell) scripting[1].
No it does not.
BeOS never had any /usr.
Haiku doesn't either.
It's just used because people "saw" env being put there more often than python or perl of whatever, but it doesn't mean they looked to all possible places.
It has just more chances of working but it's not a guarantee.
> For Python for example it's the official way to specify the interpreter.
Being official doesn't mean it's correct or works.
> I don't think "Atom Smasher" need a "guarantee" - just a confirmation
> that it does indeed work.
Again, it won't on Haiku and likely others.
François.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-20 14:01 ` Thorsten Kampe
@ 2010-07-20 14:13 ` François Revol
0 siblings, 0 replies; 29+ messages in thread
From: François Revol @ 2010-07-20 14:13 UTC (permalink / raw)
To: zsh-users
Le 20 juil. 2010 à 16:01, Thorsten Kampe a écrit :
>> Indeed, this kind of "too smart" stuff fails miserably on BeOS and Haiku, which does not have /usr.
>
> Now that is a really convincing argument. BeOS was last updated in 2001
> and "Haiku" has reached now Alpha stage after nine years. Seriously, on
Oh that's a nice way to discard both the question and consideration for non-mainstream OSes (or should I say "anything non-linux" ?).
> systems that don't follow the FHS at all, you will have a lot more
> serious problems running any kind of script.
I ran many shell/perl/python/… scripts in BeOS and Haiku for years without much trouble.
Not that much as with people thinking using autofools makes their apps automagically portable but forget to test for libm.
I've ported mercurial to BeOS & Haiku, and, coïncidentally the only thing I had to change was exactly this #!/usr/bin/env python crap.
And it works very well (unlike git which was ported but is as unusable as on linux :p).
François.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: zsh portable script
2010-07-20 14:07 ` François Revol
@ 2010-07-21 8:34 ` Thorsten Kampe
0 siblings, 0 replies; 29+ messages in thread
From: Thorsten Kampe @ 2010-07-21 8:34 UTC (permalink / raw)
To: zsh-users
* François Revol (Tue, 20 Jul 2010 16:07:32 +0200)
> Le 20 juil. 2010 à 15:53, Thorsten Kampe a écrit :
> > * Joke de Buhr (Mon, 12 Jul 2010 17:37:19 +0200)
> >> On Monday 12 July 2010 16:46:22 Atom Smasher wrote:
> >>> on freebsd, zsh installs as /usr/local/bin/zsh. on linux (and most
> >>> other systems?) it installs as /usr/bin/zsh.
> >>>
> >>> what's the best way to make zsh script portable between linux and
> >>> freebsd?
> >>>
> >>> i could start the script with:
> >>> #!/usr/bin/env zsh
> >>>
> >>> or i could specify that the script be executed as:
> >>> zsh script
> >>>
> >>> is there a better way?
> >>
> >> Using env doesn't solve the problem either. There is no guarantee the "env"
> >> program is installed under /usr/bin/env.
> >
> > It does solve the problem that's why it's used in (shell) scripting[1].
>
> No it does not.
> BeOS never had any /usr.
> Haiku doesn't either.
The question was "what's the best way to make zsh script portable
between linux and freebsd?" Why would the original poster care about OSs
which are either dead for ten years or haven't left alpha stage yet?
If you want to be generally "portable" then you wouldn't write a zsh
script in the first place: "When portability is an issue: you can rely
on the Bourne shell existing anywhere. Scripting languages like Perl and
even the newer shells like bash and zsh are potentially unavailable"
("From Bash to Z Shell" - Oliver Kiddle, Jerry Peek and Peter
Stephenson)
Trying to create a script that would magically "run everywhere" is never
going to work. For a "real life" description see here:
"While the transition to sh and general UNIX compatibility has been a
bumpy ride, it has also been a tremendous learning experience. Creating
a single script that runs on all of these platforms has been very tricky
indeed, mainly because I simply don't have access to most of these
operating systems! Thankfully, keychain users from around the globe do,
and many have provided great assistance in identifying compatibility
problems and submitting patches to fix them."[1]
All these compatibility changes come with a heavy price: they make the
code much less readable (as described under "Shell compatibility
fixes"[1])
The lesson here is: if you want a Zsh script that runs on the major
Linux distributions and on FreeBSD then create one that does exactly
that. Don't try to "over-engineer" to make it run everywhere if you
don't have the ressources (like lots of contributors) and especially:
dont't try to make it run on a platform you don't have access to (and on
which the script is never supposed to run, anyway).
Thorsten
[1] http://www.gentoo.org/doc/en/articles/openssh-key-management-p3.xml
^ permalink raw reply [flat|nested] 29+ messages in thread
end of thread, other threads:[~2010-07-21 8:35 UTC | newest]
Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-12 14:46 zsh portable script Atom Smasher
2010-07-12 15:35 ` Sebastian Stark
2010-07-12 16:10 ` Joke de Buhr
2010-07-12 16:17 ` Vincent Lefevre
2010-07-12 15:37 ` Joke de Buhr
2010-07-12 15:45 ` Joke de Buhr
2010-07-12 16:01 ` Frank Terbeck
2010-07-12 16:15 ` Vincent Lefevre
2010-07-12 16:18 ` Vincent Lefevre
2010-07-12 16:31 ` Joke de Buhr
2010-07-12 16:43 ` Vincent Lefevre
2010-07-12 16:22 ` Sebastian Stark
2010-07-12 16:39 ` Vincent Lefevre
2010-07-12 16:47 ` Frank Terbeck
2010-07-13 13:02 ` Atom Smasher
2010-07-13 14:29 ` Benjamin R. Haskell
2010-07-13 15:28 ` Atom Smasher
2010-07-13 17:01 ` Benjamin R. Haskell
2010-07-14 1:11 ` Atom Smasher
2010-07-14 11:45 ` Atom Smasher
2010-07-14 15:00 ` Atom Smasher
2010-07-13 13:43 ` François Revol
2010-07-20 14:01 ` Thorsten Kampe
2010-07-20 14:13 ` François Revol
2010-07-20 13:53 ` Thorsten Kampe
2010-07-20 14:07 ` François Revol
2010-07-21 8:34 ` Thorsten Kampe
2010-07-12 15:37 ` Vincent Lefevre
2010-07-12 16:06 ` Peter Stephenson
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).