Re: portable shell scripts (was: How do you survive without notmuch?)

2016-04-07 Thread Cameron Simpson

On 07Apr2016 22:27, derek martin  wrote:

On Thu, Apr 07, 2016 at 04:23:18PM -0700, David Champion wrote:

Speaking of which, it's taken me until the last year to use $(command)
consistently instead of `command`, and I'm not sure anymore why I was
a stickler.  I assume some older shell didn't support $() but I can't
recall which.


I still don't, as you may have noticed above. ;-)  But I DO use it,
and notably you can combine `` and $() to get two levels of execution.
That's occasionally handy.


I started getting aggressive about $() in the last year or so; it has certainly 
been around for long enough. The big attraction for me is that it nests without 
introducing more slosh-escaping levels. So you can $( ... $( ... $(...) ... ) 
... ) without stuff getting nasty. Not that I nest that deeply, but whenever I 
have a regexp or the like inside `` it is immediately more painful.


Cheers,
Cameron Simpson 


Re: portable shell scripts (was: How do you survive without notmuch?)

2016-04-07 Thread Derek Martin
On Fri, Apr 08, 2016 at 08:51:52AM +1000, Cameron Simpson wrote:
> On 07Apr2016 10:37, derek martin  wrote:
> >I've yet to notice any of the so-called benefits of getting older,
> >that people sometimes extoll...
> 
> My father once asserted that it was better than the alternative.

Fair point!  On rare occasions though, I'm not so sure...

> Oh, please digress!
> 
> I tend to use printf over echo for when I want well controlled
> output; the BSD/SysV echo stuff has been a pain point for decades so
> I try to just avoid it when I'm outputting an "unknown" string. 

I've only taken to using printf lately in shell scripts, mostly for
the likes of:

  i=0; while [ $i -lt $limit ]; do
filename=`printf "file%04d" $i`
# do something with file0xxx
...

> What other scripting shortcomings have you encountered with dash? I
> haven't used it much myself, but would hope to have my scripts
> portable enough to work with it.

Honestly I can't recall...  It's been a few years since I used shell
scripts for much more than stringing together a bunch of other
programs I've written or short one-offs.  One thing is I have a
handful of scripts I use a lot that make use of bash arrays to convert
numeric input into strings to be used in SQL queries.  Bash is hardly
the only shell that provides arrays... I want them in shells I use for
scripting, and frankly I want to use /bin/sh (or at least a shell that
behaves exactly like I expect /bin/sh to behave)...

I specifically DO NOT want to use /bin/bash in my shell scripts,
because this changes various behaviors of the shell, including causing
it to read certain of your shell startup files when your script starts
(yes, even in non-interactive shells), which is not what you want if
you're running shell scripts.

Honestly, what I really want is for the distros to return bash to
/bin/sh, configure the kernel and any relevant startup scripts to use
dash directly if that's what they want to use to start the system up,
and maybe consider statically linking /bin/bash so that it doesn't
break if shared libs are missing (which it may already be, I'm too
lazy to open a window to check).  =8^)

On Thu, Apr 07, 2016 at 04:23:18PM -0700, David Champion wrote:
> Speaking of which, it's taken me until the last year to use $(command)
> consistently instead of `command`, and I'm not sure anymore why I was
> a stickler.  I assume some older shell didn't support $() but I can't
> recall which.

I still don't, as you may have noticed above. ;-)  But I DO use it,
and notably you can combine `` and $() to get two levels of execution.
That's occasionally handy.

-- 
Derek D. Martinhttp://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address.  Replying to it will result in
undeliverable mail due to spam prevention.  Sorry for the inconvenience.



pgpyURJxa9152.pgp
Description: PGP signature


Re: portable shell scripts (was: How do you survive without notmuch?)

2016-04-07 Thread David Champion
What fun!

* On 07 Apr 2016, Cameron Simpson wrote: 
> 
> Ah. I like zsh for my interactive shell. But we always leave root's shell
> alone. However, I am a _very_ strong advocate for writing scripts for
> /bin/sh, and avoiding bashisms for exactly your reasons above. Am I alone in
> wincing everytime I hear the term "bash scripting" these days?

No, you're not. :) I haven't brought myself to use zsh yet, because
I do a lot of /bin/sh snippet teseting in my interactive shell and I
like it to interpret bourne code correctly.  Bash does, but zsh does
not -- even with all the compatibility options set.  There are certain
bourne sequences that zsh does the wrong thing with, and that affects my
scripting.


> > Though, the
> > switch in many Linux distros to dash as the system shell has somewhat
> > thwarted me--dash isn't (IMO) usable as an interactive shell, has some
> > issues as a scripting shell, and has slightly different semantics (BSD
> > vs.  SysV echo, for instance) than bash on some oft-used builtins,
> > etc..  I find this extremely annoying.  I get why they did it, but I
> > think it would have been a much better idea to produce a
> > bash-workalike shell for the subset of features they wanted to
> > support.  Or patch dash to behave the same where its supported
> > features differ.  Anyway, I digress.
> 
> Oh, please digress!

I tend to like dash, although my experience of it is limited.  I didn't
like ash but dash seems pretty solid so far.  And its code is quite
readable.  This is excellent.  Bash is a morass.


> but printf for parameterised output:
> 
>  printf '%s\n' "$arbitrary_value"

I've started leaning on printf for newline-less printing lately -- it's
just easier.  But you must be careful to use 'printf %s "$foo"' instead
of just 'printf $foo', since otherwise a % in input can thwart.

> and I have my own script "necho" for "echo with no newline" for the
> appropriat platform (or I make a shell function necho() calling printf for
> the same purpose, depending on context). Then one can go:

Let's see if I can get this right from memory:
case "`echo -n`" in)
-n) necho () { echo "$@""\c"; };;
*)  necho () { echo -n "$@"; };;
esac

Speaking of which, it's taken me until the last year to use $(command)
consistently instead of `command`, and I'm not sure anymore why I was
a stickler.  I assume some older shell didn't support $() but I can't
recall which.

-- 
David Champion • d...@bikeshed.us


portable shell scripts (was: How do you survive without notmuch?)

2016-04-07 Thread Cameron Simpson

On 07Apr2016 10:37, derek martin  wrote:

I've yet to notice any of the so-called benefits of getting older,
that people sometimes extoll...


My father once asserted that it was better than the alternative.


Probably fixed in modern interactive shells. Note your test is
testing your interactive shell, not /bin/sh (if they're different).


On the system I ran the test on, they happen to be the same.  On other
systems I use /bin/sh is dash, but I believe it behaves the same.
My sysadmin background has led me to think that you should always just
use the system default shell, because it's pretty much always going to
be available unless the system is completely unusable, and because
perhaps there's a good reason it's the default. ;-)


Ah. I like zsh for my interactive shell. But we always leave root's shell 
alone. However, I am a _very_ strong advocate for writing scripts for /bin/sh, 
and avoiding bashisms for exactly your reasons above. Am I alone in wincing 
everytime I hear the term "bash scripting" these days?



Though, the
switch in many Linux distros to dash as the system shell has somewhat
thwarted me--dash isn't (IMO) usable as an interactive shell, has some
issues as a scripting shell, and has slightly different semantics (BSD
vs.  SysV echo, for instance) than bash on some oft-used builtins,
etc..  I find this extremely annoying.  I get why they did it, but I
think it would have been a much better idea to produce a
bash-workalike shell for the subset of features they wanted to
support.  Or patch dash to behave the same where its supported
features differ.  Anyway, I digress.


Oh, please digress!

I tend to use printf over echo for when I want well controlled output; the 
BSD/SysV echo stuff has been a pain point for decades so I try to just avoid it 
when I'm outputting an "unknown" string. So I use echo for all the fixed stuff:


 echo "message here, with no leading dashes or backslashes in the text"

but printf for parameterised output:

 printf '%s\n' "$arbitrary_value"

and I have my own script "necho" for "echo with no newline" for the appropriat 
platform (or I make a shell function necho() calling printf for the same 
purpose, depending on context). Then one can go:


 necho "prompt> "

in one's script without worrying too much or making the script harder to read.

What other scripting shortcomings have you encountered with dash? I haven't 
used it much myself, but would hope to have my scripts portable enough to work 
with it.


Cheers,
Cameron Simpson