Codes
Answers
#!/bin/sh
die() {
echo "${0##*/}: error: $*" >&2
exit 1
}
usage() {
echo "Usage: foo [options] [args]
This does something useful!
Options:
-o <file> Write output to <file>
-v Run verbosely
-h This help screen"
exit 0
}
main() {
local flag
local verbose="false"
local out="/dev/stdout"
while getopts 'ho:v' flag; do
case ${flag} in
h) usage ;;
o) out="${OPTARG}" ;;
v) verbose="true" ;;
*) die "invalid option found" ;;
esac
done
if [[ ${verbose} == "true" ]]; then
echo "verbose mode is enabled!"
else
echo "will be quiet"
fi
if [[ -z ${out} ]]; then
die "-o flag is missing"
fi
echo "writing output to: '${out}'"
# Now remaining arguments are in "$@".
...
}
main "$@"
Questions
Answers
Answer accepted
Users
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us