Suppose you have the following problem:
Depending on if a script is executed within a pipe or not, you want to append the -print0
parameter to find
.
I can think of at least two ways to do this:
[[ -p '/dev/stdin' ]] && local null_terminated='-print0'
find -mindepth 1 -maxdepth 1 ! \( "${args[@]}" \) "$null_terminated"
Or
if [[ -p '/dev/stdin' ]]; then
find -mindepth 1 -maxdepth 1 ! \( "${args[@]}" \) -print0
else
find -mindepth 1 -maxdepth 1 ! \( "${args[@]}" \)
fi
Is there any better way to do this? The question is for bash
, but solutions for other shells or portable/standard sh
solutions would be welcome as well.