I've an question, how to use *if *or *case * statements to do below command :
command.sh --value string ?
I tried :
case "$1" in
[--value ])
echo "You choose value"
;;
*)
echo "plz chs diff one"
;;
esac
But it is not what I want to achieve. I want to run command.sh --value string , where string is the first argument.
CodePudding user response:
getopts
is a tool that can be used to parse arguments
usage() {
echo "Usage: $0 [-v]" 1>&2
}
exit_abnormal() {
usage
exit 1
}
while getopts "v" arg; do
case $arg in
v) USE_VALUE='true' ;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
;;
esac
done
shift
if [[ $USE_VALUE == "true" ]]; then
...
fi
CodePudding user response:
You can try this:
if [ "$1" = "--value" ]; then
echo "You choose value $2"
else
echo "plz chs diff one"
fi