aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2018-04-23 20:42:40 -0700
committerChristine Dodrill <me@christine.website>2018-04-23 20:42:40 -0700
commit617cede28dbb2c35546d4c6ccd6a150ff7c1ada5 (patch)
tree2f4dc9ec2fdfed15842027123a6858e14915730f
parent01501d25223e9b8bef0337e0f2edb98c36ba1397 (diff)
downloadxesite-617cede28dbb2c35546d4c6ccd6a150ff7c1ada5.tar.xz
xesite-617cede28dbb2c35546d4c6ccd6a150ff7c1ada5.zip
blog: new post: The Beautiful in the Ugly
-rw-r--r--blog/beauty-in-ugly-2018-04-23.markdown88
1 files changed, 88 insertions, 0 deletions
diff --git a/blog/beauty-in-ugly-2018-04-23.markdown b/blog/beauty-in-ugly-2018-04-23.markdown
new file mode 100644
index 0000000..1ead059
--- /dev/null
+++ b/blog/beauty-in-ugly-2018-04-23.markdown
@@ -0,0 +1,88 @@
+---
+title: The Beautiful in the Ugly
+date: 2018-04-23
+for: Silver
+---
+
+# The Beautiful in the Ugly
+
+Functional programming is nice and all, but sometimes you just need to have
+things get done regardless of the consequences. Sometimes a dirty little hack
+will suffice in place of a branching construct. This is a story of one of these
+times.
+
+In shell script, bare words are interpreted as arbitrary commands for the shell
+to run, interpreted in its rules (simplified to make this story more interesting):
+
+1. The first word in a command is the name or path of the program being loaded
+2. Variable expansions are processed before commands are executed
+
+Given the following snippet of shell script:
+
+```shell
+#!/bin/sh
+# hello.sh
+
+function hello {
+ echo "hello, $1"
+}
+
+$1 $2
+```
+
+When you run this without any arguments:
+
+```console
+$ sh ./hello.sh
+$
+```
+
+Nothing happens.
+
+Change it to the following:
+
+```console
+$ sh ./hello.sh hello world
+hello, world
+$ sh ./hello.sh ls
+hello.sh
+```
+
+Shell commands are bare words. Variable expansion can turn into execution.
+Normally, this is terrifying. This is useful in fringe cases.
+
+Consider the following script:
+
+```shell
+#!/bin/sh
+# build.sh <action> [arguments]
+
+projbase=github.com/Xe/printerfacts
+
+function gitrev {
+ git rev-parse HEAD
+}
+
+function app {
+ export GOBIN="$(pwd)"/bin
+ go install github.com/Xe/printerfacts/cmd/printerfacts
+}
+
+function install_system {
+ app
+
+ cp ./bin/printerfacts /usr/local/bin/printerfacts
+}
+
+function docker {
+ docker build -t xena/printerfacts .
+ docker build -t xena/printerfacts:"$(gitrev)"
+}
+
+function deploy {
+ docker tag xena/printerfacts:"$(gitrev)" registry.heroku.com/printerfacts/web
+ docker push registry.heroku.com/printerfacts/web
+}
+
+$*
+``` \ No newline at end of file