Re: [dev] philosophy of the s shell

From: <mail_AT_josuah.net>
Date: Mon, 10 Apr 2017 21:25:14 +0200

I like the idea of only having commands, environment variables, pipes,
and conditions (with && and ||). This has some purity, not using the
shell as a programming language, but as a tool to compose other
programs.

I was worried about a few things not being possible at all, like:

        find . -type f | xargs md5sum | while read -r hash path
        do
                cp "$path" "obj/$hash"
        done

But then with ...

        ... | xargs -n 2 cp

... it could work with the odd lines as the sources and even lines as
the destinations:

        find . -type f | xargs md5sum |
        sed -r 's|([^ ]*) (.*)|\2\nobj/\1|' |

        # ./obj/f73696e15dc73e463be705e132df9416
        # ./etc/bin/mb-filter
        # ./obj/08e1b3a3ff3a4690d651fbbe31b90289
        # ./etc/bin/mb-all
        # ./obj/4da6918e25e2010cb2612f3378678307
        # ./etc/bin/ii-filter
        # ...

        xargs -n 2 cp

This is still sh(1)-compatible.

Now what if I want to add a condition in-between?

        find . -type f | xargs md5sum | while read -r hash path
        do
                [ -f "obj/$path" ] || cp "$path" "obj/$hash"
        done

I could play hard with find:

        xargs -I % -n 1 find % -exec test -f {} \; -print

Or I could use non-portable cp -n option which make the test, but
stest(1) is a better approach.

But I have to pipe only the odd lines through it, and keeping the even
lines only if the odd lines did not match.

I could also call s(1) again with xargs, and use another script with
positionnal parameter if s(1) support them (or a -c flag as in sh(1)):

        test -f obj/$2 || cp $1 obj/$2


Finally: can it redirect stout to stderr? A stand alone program could
do this:

        errout CMD ARGS

So this seems to work. It is possible to reduce shell scripts to mostly
a single pipe for most things. s(1) it then "Just enough". Moreover,
in command-line, I quite never do anything else, so s(1) is perfect for
interactive use for me.

I would still prefer that linenoise was not required, using rlwrap
instead:

        rlwrap dash # bonus: rlwrap sic

behaves mostly like bash or other bloated shell.

Thank you for s, it is for me an interesting direction to follow.

rlwrap(1): http://github.com/hanslub42/rlwrap
Received on Mon Apr 10 2017 - 21:25:14 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 12 2017 - 12:12:05 CEST