aboutsummaryrefslogtreecommitdiff
path: root/bin/x
diff options
context:
space:
mode:
Diffstat (limited to 'bin/x')
-rwxr-xr-xbin/x94
1 files changed, 94 insertions, 0 deletions
diff --git a/bin/x b/bin/x
new file mode 100755
index 0000000..ed967a5
--- /dev/null
+++ b/bin/x
@@ -0,0 +1,94 @@
+#!/bin/sh
+set -eu
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ x COMMANDS... [ '&&' / '||' / '|' ] COMMANDS...
+ x -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+ Options:
+ -h, --help show this message
+
+ COMMAND the command to be executed
+ && "AND" logical operator
+ || "OR" logical operator
+ | pipe operator
+
+
+ Run command chained together with operators.
+
+
+ Examples:
+
+ Measure the time of two commands:
+
+ $ time x sleep 1 '&&' sleep 2
+ # same as:
+ $ time sh -c 'sleep 1 && sleep 2'
+
+
+ Notify when either of the commands finish:
+
+ $ boop x cmd-1 '||' cmd-2
+ EOF
+}
+
+for flag in "$@"; do
+ case "$flag" in
+ --)
+ break
+ ;;
+ --help)
+ usage
+ help
+ exit
+ ;;
+ *)
+ ;;
+ esac
+done
+
+while getopts 'h' flag; do
+ case "$flag" in
+ h)
+ usage
+ help
+ exit
+ ;;
+ *)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+
+CMD=''
+for arg in "$@"; do
+ case "$arg" in
+ '&&'|'||')
+ set +e
+ sh -c "$CMD"
+ STATUS=$?
+ set -e
+ CMD=''
+ if [ "$arg" = '&&' ] && [ "$STATUS" != 0 ]; then
+ exit "$STATUS"
+ elif [ "$arg" = '||' ] && [ "$STATUS" = 0 ]; then
+ exit 0
+ fi
+ ;;
+ *)
+ CMD="$CMD${CMD:+ }'$arg'"
+ ;;
+ esac
+done
+
+sh -c "$CMD"