I have the following snippet:
#!/bin/bash
OPTIND=1
while getopts ":m:t" params; do
case "${params}" in
m)
bar=$OPTARG ;;
t)
foo=$OPTARG ;;
\?)
"Invalid option: -$OPTARG" >&2
print_usage
exit 2
;;
:)
echo "Option -$OPTARG requires an argument." >&2
print_usage
exit 2
;;
esac
done
shift "$(( OPTIND-1 ))"
echo "${foo}" && echo "${bar}"
How can I output the stdout using pipes through this script?
For example:
echo "this is the test" | bash getoptscript.sh -m -
And it should provide :
this is the test as the output.
echo "Test" | xargs bash getoptscript.sh -m. But is this "good" way?-and otherwise use the argument itself? That should be a simple test within them)case, read from stdin ifOPTARGis-, otherwise useOPTARGitself. I'm not exactly sure if this is what you wanted, though. It would seem more common to have dash as a filename representing stdin. Some commands have a different option characters for giving the literal value and for giving a reference to a file from which to read it