Last active
April 3, 2021 16:55
-
-
Save liliankasem/2fb69d50062c3c7901b191919b7390a7 to your computer and use it in GitHub Desktop.
Bash script example arguments / flags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| opts_count=0 | |
| while getopts g:n: flag | |
| do | |
| case "${flag}" in | |
| g) greeting=${OPTARG}; ((opts_count=opts_count+1)) ;; | |
| n) name=${OPTARG}; ((opts_count=opts_count+1)) ;; | |
| *) echo "Unknown parameter passed"; exit 1 ;; | |
| esac | |
| done | |
| usage(){ | |
| echo "**Sample Script***" | |
| echo "Usage: ./script.sh -g Hello -n World" | |
| echo "---Parameters---" | |
| echo "g= :greeting" | |
| echo "n= :name" | |
| } | |
| start() { | |
| echo "$greeting $name" | |
| } | |
| # Exit program if we don't get arguments | |
| [[ $opts_count < 2 ]] && { usage && exit 1; } || start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment