Fadak.IR Fadak Solutions
English Русский العربية فارسی
Articles Management Studies Language


/ Coumputer / Programming

Bash (Unix shell) - Bash is the GNU Project s shell


Getopts in bash(Get Option)

#!/bin/bash
usage() { echo "Usage: $0 [-s <45|90>] [-p ]" 1>&2; exit 1; }
while getopts ":s:p:" o; do
case "${o}" in
s)
s=${OPTARG}
((s == 45 || s == 90)) || usage
;;
p)
p=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${s}" ] || [ -z "${p}" ]; then
usage
fi
echo "s = ${s}"
echo "p = ${p}"
Example runs:
$ ./myscript.sh
Usage: ./myscript.sh [-s <45|90>] [-p ]
$ ./myscript.sh -h
Usage: ./myscript.sh [-s <45|90>] [-p ]
$ ./myscript.sh -s "" -p ""
Usage: ./myscript.sh [-s <45|90>] [-p ]
$ ./myscript.sh -s 10 -p foo
Usage: ./myscript.sh [-s <45|90>] [-p ]
$ ./myscript.sh -s 45 -p foo
s = 45
p = foo
$ ./myscript.sh -s 90 -p bar
s = 90
p = 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

 

 

Variable Assignment

 

 

foo="${foo} World"
echo "${foo}"

 

 

Variable Assignment


Articles
Digital Media
Humanities
IT Management
Coumputer
Miscellaneous
Product & Services
About Fadak
Management
Contemporary Management Journal
Managerial Verses
Photography Quotes
Photo is written
Management Researcher Bank
Management articles titles
Educational Resources (Seminary & University)
Studies
Observatory - Personalities
Observatory - Cultural
Observatory - Academic
Observatory - Media
Observatory - scientific events
Language
Dictionary
Russian Language Test
Russian Proverb
English Proverb
Four language sentences
logo-samandehi
About | Contact With Us | Privacy Policy | Terms | Cookies Policy |
Version (Pre-Alpha) 2000-2022 CMS Fadak. ||| Version : 5.2 ||| By: Fadak Solutions Old Version