I was trying to do this to decide whether to redirect stdin to a file or not:
[ ...some condition here... ] && input=$fileName || input=\"&0\"
./
How about
function runfrom {
local input="$1"
shift
case "$input" in
-) "$@" ;;
*) "$@" < "$input" ;;
esac
}
I've used the minus sign to denote standard input because that's traditional for many Unix programs.
Now you write
[ ... condition ... ] && input="$fileName" || input="-"
runfrom "$input" my-complicated-command with many arguments
I find these functions/commands which take commands as arguments (like xargs(1)
) can be very useful, and they compose well.