#!/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"