diff options
author | EuAndreh <eu@euandre.org> | 2022-08-16 22:59:42 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-08-16 22:59:45 -0300 |
commit | 569fd1018f289fe14fd6ffd48fb671e0849b5c16 (patch) | |
tree | f962ce6bcd5f6f57d6d38ca464e96d2c995d95f1 /bin | |
parent | bin/menu: Implement "yubikey" ACTION using ykman (diff) | |
download | dotfiles-569fd1018f289fe14fd6ffd48fb671e0849b5c16.tar.gz dotfiles-569fd1018f289fe14fd6ffd48fb671e0849b5c16.tar.xz |
bin/x: Add semi-working version
No support for the '|' pipe operator.
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/x | 94 |
1 files changed, 94 insertions, 0 deletions
@@ -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" |