blog |

test Builtin Style Guide


We saw that [bash][] has many redundant constructs, like:

In contrast, Oil will have "one way to do it". But I want to automatically convert shell programs written in a variety of styles.

Oil doesn't need to drag this legacy along!!! The 6 case algorithm.

if (s > t) { # greater than less than. This depends on LOCALE. Maybe change # it to a function? } if (s == t) {

Disabled: ! -a -o ( ) < >

a = ' 3 ' # note spaces, we read them from a file b = ' 5 ' test $a -lt $b

if (a < b) # BAD: STRINGS THAT LOOK LIKE NUMBERS # maybe disallow this, too subtle! # I would have to write a comment

if (sortsBefore(a, b)) if (order(a, b)) if (cmp(a, b)) if (cmpLocale(a, b))

if (Int(a) < Int(b)) # THis is OK

Shell Style Guideline: For Oil Translation

I don't think this style guideline is very restrictive. In fact I never used [[ -- I've been writing shell scripts for 10 years. The 2 and 3 argument versions of test suffice for almost all purposes.

Options for Oil:

Style guideline / Oil:

just use two arg or three args:o

test -f "$path" ->

test -file $path # not quoting, get rid of

test is-file $path test is-dir $path test exists $path test is-pipe $path

alternative if (isFile(path)), if (isDir)

test $path older-than $path && test $path newer-than $path # with auto-complete test $path is-hard-link-to $path # with auto-complete

Philosophy: Compatible vs. Nice Translations

Because OSH will implement essentially all shell builtins, it is trivial to make Oil compatible. But we don't want the Oil language to be burdened by compatibility -- that's how we ended up with [ -a -a -a] in the first place!

So if you follow our (loose) style guidelines, you'll get the nice translation.

If you don't, you'll get the "compatible translation", with __. The __ is a visual cue that you could manually rewrite some code to be nicer in Oil.

Example Oil Translations

These are just what I'm thinking; they haven't been implemented yet.

You must follow the style guidelines above. However, I still want to retain the property of automatic conversion. So I'm thinking of having a namesapce for shell builtins in Oil.

Refer to Translating Shell to Oil.

if _ test -a -a -a {

_ could be old builtins. It's subtle a sign that something could be "modernized".

Another option would be if eval-sh "test -a -a -a" {} , but this seems too ugly.

Other options:

if $ test -a -a -a -{

if $$ test -a -a -a -{

if __ test -a -a -a -{

while __ read -r foo {

Or you could also do: