From 569fd1018f289fe14fd6ffd48fb671e0849b5c16 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Tue, 16 Aug 2022 22:59:42 -0300 Subject: bin/x: Add semi-working version No support for the '|' pipe operator. --- bin/x | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 bin/x (limited to 'bin/x') 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" -- cgit v1.2.3