Why Sponsor Oils? | blog | oilshell.org

Example Code in Shell, Awk, and Make

2016-11-14

Yesterday I wrote about combining Shell, Awk, and Make. Today we show the a program written in each language, showing their similar ALGOL-like features:

The programs do the same thing, but their syntax is wildly different. Although I know the languages, I still consulted Google several times for each example.

hello-func.sh
f() {
  echo "hello $1";
}
if test -n "$NAME"; then
  f $NAME
else
  for i in $(seq 3); do
    f $i
  done
fi
hello-func.awk
function f(name) {
  print "hello " name
}
BEGIN {
  if (ENVIRON["NAME"]) {
    f(ENVIRON["NAME"])
  } else {
    for (i = 1; i <= 3; ++i) {
      f(i)
    }
  }
}
hello-func.mk
define f
	echo hello $(1)

endef

default:
ifdef NAME
	$(call f,$(NAME))
else
	$(foreach i,1 2 3,$(call f,$i))
endif

The programs have the same output when run with NAME unset:

$ sh demo/hello-func.sh
hello 1
hello 2
hello 3
$ awk -f demo/hello-func.awk </dev/null
hello 1
hello 2
hello 3
$ make --quiet -f demo/hello-func.mk
hello 1
hello 2
hello 3

And with NAME set:

$ NAME=world sh demo/hello-func.sh
hello world
$ NAME=world awk -f demo/hello-func.awk </dev/null
hello world
$ NAME=world make --quiet -f demo/hello-func.mk
hello world

Next Steps

I've written about eight projects that oil can parse, the last one being git. It now parses a dozen more without error or with only known problems like the undecidable parse.

So it's time to release the parser -- I hope to do that in the next week or so. Then I can start writing about the Oil language . I introduced the idea of combining Shell, Awk, and Make in the last two posts so I can talk about their influence on its design.