-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
promote
eval-on-empty-stdin
, eval-on-not-empty-stdin
- Loading branch information
Showing
4 changed files
with
126 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env bash | ||
|
||
function eval_on_empty_stdin() ( | ||
source "$DOROTHY/sources/bash.bash" | ||
|
||
# ===================================== | ||
# Arguments | ||
|
||
function help { | ||
cat <<-EOF >/dev/stderr | ||
ABOUT: | ||
Execute the <command> if stdin is empty; stdin content will continue to be directed to stdout. | ||
Companion to [eval-on-not-empty-stdin]. | ||
USAGE: | ||
eval-on-empty-stdin [--] ...<command> | ||
EOF | ||
if [[ $# -ne 0 ]]; then | ||
echo-error "$@" | ||
fi | ||
return 22 # EINVAL 22 Invalid argument | ||
} | ||
|
||
# process | ||
local item option_cmd=() | ||
while [[ $# -ne 0 ]]; do | ||
item="$1" | ||
shift | ||
case "$item" in | ||
'--help' | '-h') help ;; | ||
'--') | ||
option_cmd+=("$@") | ||
shift $# | ||
break | ||
;; | ||
'--'*) help "An unrecognised flag was provided: $item" ;; | ||
*) | ||
option_cmd+=("$item" "$@") | ||
shift $# | ||
break | ||
;; | ||
esac | ||
done | ||
|
||
# ===================================== | ||
# Action | ||
|
||
# doesn't work: | ||
# if read -r; then | ||
# __print_lines 'not empty' | ||
# else | ||
# __print_lines 'empty' | ||
# fi | ||
|
||
# doesn't work: | ||
# if [[ -t 0 ]]; then | ||
# __print_lines 'empty' | ||
# else | ||
# __print_lines 'not empty' | ||
# fi | ||
|
||
# works: | ||
setup-util-moreutils --quiet # ifne | ||
ifne -n "${option_cmd[@]}" | ||
return | ||
) | ||
|
||
# fire if invoked standalone | ||
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then | ||
eval_on_empty_stdin "$@" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env bash | ||
|
||
function eval_on_not_empty_stdin() ( | ||
source "$DOROTHY/sources/bash.bash" | ||
|
||
# ===================================== | ||
# Arguments | ||
|
||
function help { | ||
cat <<-EOF >/dev/stderr | ||
ABOUT: | ||
Execute the <command> if stdin is not empty; the stdin content will still be available to whatever eventually reads it. | ||
Companion to [eval-on-empty-stdin]. | ||
USAGE: | ||
eval-on-not-empty-stdin [--] ...<command> | ||
EOF | ||
if [[ $# -ne 0 ]]; then | ||
echo-error "$@" | ||
fi | ||
return 22 # EINVAL 22 Invalid argument | ||
} | ||
|
||
# process | ||
local item option_cmd=() | ||
while [[ $# -ne 0 ]]; do | ||
item="$1" | ||
shift | ||
case "$item" in | ||
'--help' | '-h') help ;; | ||
'--') | ||
option_cmd+=("$@") | ||
shift $# | ||
break | ||
;; | ||
'--'*) help "An unrecognised flag was provided: $item" ;; | ||
*) | ||
option_cmd+=("$item" "$@") | ||
shift $# | ||
break | ||
;; | ||
esac | ||
done | ||
|
||
# ===================================== | ||
# Action | ||
|
||
setup-util-moreutils --quiet # ifne | ||
ifne "${option_cmd[@]}" | ||
) | ||
|
||
# fire if invoked standalone | ||
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then | ||
eval_on_not_empty_stdin "$@" | ||
fi |