myscript.sh
#!/bin/bash
org=""
name=""
# Define the usage function
usage() {
echo "Usage: $0 [-o|--org <org>] [-n|--name <name>] [-h|--help]"
exit 1
}
# Define the help function
help() {
echo "This script performs a task using org and name options."
echo "Options:"
echo " -o, --org Specify the org value"
echo " -n, --name Specify the name value"
echo " -h, --help Show this help message"
exit 0
}
# Use getopt to parse command line options
opts=$(getopt -o o:n:h --long org:,name:,help -n "$0" -- "$@")
if [ $? != 0 ]; then
usage
fi
echo "####################"
echo $opts
eval set -- "$opts"
while true; do
case "$1" in
-o | --org)
org="$2"
shift 2
;;
-n | --name)
name="$2"
shift 2
;;
-h | --help)
help
;;
--)
shift
break
;;
*)
usage
;;
esac
done
# Additional code here based on the parsed options
echo "org: $org"
echo "name: $name"
标签:传参,shell,name,--,echo,usage,org,模板,help
From: https://www.cnblogs.com/xwjh/p/17787749.html