Last active
September 19, 2024 05:27
-
-
Save hfossli/4368aa5a577742c3c9f9266ed214aa58 to your computer and use it in GitHub Desktop.
Standard bash script format
This file contains 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 | |
CLEAR='\033[0m' | |
RED='\033[0;31m' | |
function usage() { | |
if [ -n "$1" ]; then | |
echo -e "${RED}👉 $1${CLEAR}\n"; | |
fi | |
echo "Usage: $0 [-n number-of-people] [-s section-id] [-c cache-file]" | |
echo " -n, --number-of-people The number of people" | |
echo " -s, --section-id A sections unique id" | |
echo " -q, --quiet Only print result" | |
echo "" | |
echo "Example: $0 --number-of-people 2 --section-id 1 --cache-file last-known-date.txt" | |
exit 1 | |
} | |
# parse params | |
while [[ "$#" > 0 ]]; do case $1 in | |
-n|--number-of-people) NUMBER_OF_PEOPLE="$2"; shift;shift;; | |
-s|--section-id) SECTION_ID="$2";shift;shift;; | |
-v|--verbose) VERBOSE=1;shift;; | |
*) usage "Unknown parameter passed: $1"; shift; shift;; | |
esac; done | |
# verify params | |
if [ -z "$NUMBER_OF_PEOPLE" ]; then usage "Number of people is not set"; fi; | |
if [ -z "$SECTION_ID" ]; then usage "Section id is not set."; fi; |
You could also do shift 2
to shift arguments by 2 positions ( instead of doing a shift two times )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For reference, this is from: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash#comment86437551_33826763