Why Sponsor Oils? | blog | oilshell.org
This is an outline of the most important series in this blog. As I wrote on Hacker News, I started this project nearly 4 years ago with a plan:
Unfortunately there are only two posts in that series, written over 3 years ago. Step 2 is taking longer than expected!
So while I'm chipping away at Oil's implementation, here are the main ideas. These ideas should be useful for experienced shell users, and I hope to explain them more carefully to non-shell users later in 2020.
Thanks to reader enriquto
for reminding
me to write this.
Each section below describes an article I'd like to write. The heading is the proposed title, and the body has the main ideas and important URLs.
run.sh
Scriptsrun.sh
scripts consist of a bunch of shell functions followed by "$@"
.
That idiom runs the function $0
with arguments $1
, $2
, etc.
build
, test
, and deploy
. There are dozens of
shell scripts in the Oil
repo
that follow this pattern..PHONY
targets, but I prefer
shell.I used to call these scripts do.sh
, but that name shares a prefix with the
common doc/
directory, creating a speed bump for autocompletion.
2/26 update: I'm not the only one who advocates this pattern:
test
, deploy
, etc. functions, offering help and
autocompletion. Coincidentally, the author was my officemate at Google about
13 years ago. (Hi, Mike!) Back then, I was working more on migrating away
from shell than migrating toward it :) He cites these posts:
run.sh
.
March 2022 Update:
Replacing make with a Shell Script for Running Your Project's Tasks (nickjanetakis.com
via lobste.rs)
5 points, 24 comments on 2022-03-06
Github has a related pattern called Scripts to Rule Them All (2015):
Being able to get from
git clone
to an up-and-running project in a development environment is imperative for fast, reliable contributions.
linguist
repo: https://github.com/github/linguist/tree/master/script$0
-Dispatch Pattern Solves Three Important ProblemsA shell script is a simple way of documenting project-specific dev tools. That is, if you have a text file that describes a bunch of shell commands (e.g. a "playbook"), you can turn it into a shell script:
#
before the descriptions, turning them into comments.run.sh
pattern from the last post. It's nice to
have executable documentation, even if it's only semi-automated.The pattern also lets you expose shell functions to tools like xargs
/ find -exec
, and sudo / chroot.
proc
keyword, since they're
unlike functions in Python or JavaScript.)sudo $0 myfunc "$@"
is concatenative.cat tasks.txt | my-filter | xargs -- $0 myfunc "$@"
— in Oil's own tests and
benchmarks, for example.$0
.export -f
was supposed to solve. This construct caused the infamous
ShellShock bug.The $0
-dispatch pattern solves the ignored errexit
problem.
if myfunc
with if $0 myfunc
. (This advice is probably
new/unique. Leave a comment if you want details before I
publish this post.)Related points:
An HTTP daemon is such a simple thing that a simple shell script will often suffice.
— Tim Berners-Lee, inventor of the World Wide Web
/bin/sh
. Berners-Lee parses the GET
verb and a
URL with the shell's read
builtin, and cat
s the file back to the network.
(This practice ignores escaping, which I wrote about in
2017.)The Unix philosophy of combining heterogeneous tools remains useful and powerful today. Building sites with tools rather than libraries leads to efficient, flexible, and loosely coupled code.
I built this website with classic Unix tools and my own tools
written in Python. The main()
is always in shell, not Python.
Some direct analogies between HTML and shell:
I've noticed that some alternative shells on the
wiki don't have all
the good parts of shell. This is often because they don't directly control the
file descriptor table of a
process, which is done with low-level system calls like dup2()
and fcntl()
.
That is, they aren't Unix shells in the classic sense.
A couple years ago, I wrote a challenge for alternative shells (and Perl). How do you express this simple program?
f() {
echo ---
ls /
echo ---
}
f > out.txt
f | wc -l
It's very related to the Git Log in HTML post, which adds proper escaping.
print-html-listing() {
local dir="$1"
echo '<html>'
echo ' <body>'
ls "$dir" | escape-html
echo ' </body>'
echo '</html>'
}
print-html-listing > listing.txt
I recently learned that Elvish does
well on this
challenge with a
"virtual" file descriptor table. However, it doesn't have the POSIX semantics
of "copying" the shell's memory with fork()
.
I'd like to do a survey of other shells. Let me know if you'd like to help test an alternative shell or scripting language.
This post is two levels deep on the "blog stack". After releasing Oil 0.8.pre1, I drafted an announcement:
But it needed context, so I started writing:
This post summarized just one comment, mentioned in the first paragraph. The next two posts will pop the stack, summarizing more comments and describing the latest release.