Getopts in bash(Get Option)
#!/bin/bashusage() { echo "Usage: $0 [-s <45|90>] [-p ]" 1>&2; exit 1; }while getopts ":s:p:" o; docase "${o}" ins)s=${OPTARG}((s == 45 || s == 90)) || usage;;p)p=${OPTARG};;*)usage;;esacdoneshift $((OPTIND-1))if [ -z "${s}" ] || [ -z "${p}" ]; thenusagefiecho "s = ${s}"echo "p = ${p}"Example runs:$ ./myscript.shUsage: ./myscript.sh [-s <45|90>] [-p ]$ ./myscript.sh -hUsage: ./myscript.sh [-s <45|90>] [-p ]$ ./myscript.sh -s "" -p ""Usage: ./myscript.sh [-s <45|90>] [-p ]$ ./myscript.sh -s 10 -p fooUsage: ./myscript.sh [-s <45|90>] [-p ]$ ./myscript.sh -s 45 -p foos = 45p = foo$ ./myscript.sh -s 90 -p bars = 90p = bar
Other:
Parsing bash script options with getopts - Kevin Sookocheff
How do I parse command line arguments in Bash? - Stack Overflow
Bash: Argument Parsing. One parser to rule them all | by Drew Stokes | Medium
shell - An example of how to use getopts in bash - Stack Overflow
getopt -- external command to parse shell file options
foo="${foo} World"
echo "${foo}"