Skip to content

Instantly share code, notes, and snippets.

@angelovangel
Forked from rragundez/args_script_template.sh
Created September 12, 2023 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovangel/5880068729113c3caeccd7e54cc9bb42 to your computer and use it in GitHub Desktop.
Save angelovangel/5880068729113c3caeccd7e54cc9bb42 to your computer and use it in GitHub Desktop.
Template of bash script with mandatory and optional arguments
#!/bin/bash
set -e
usage="$(basename "$0") [-h] [-i PROJECT] [-v VM] [-p PYTHON] [-d NOTEBOOKS]
Make a user provide SSH key and jupyter notebooks (in roles/bootstrap/files/notebooks) to each user listed in var/common.yml
where:
-h show this help text
-i google cloud project id
-v name of instance/virtual machine
-p python path
-d if want to copy a notebooks directory"
# constants
PYTHON=/usr/bin/python3
NOTEBOOK_DIR=false
options=':hi:v:p:d:'
while getopts $options option; do
case "$option" in
h) echo "$usage"; exit;;
i) PROJECT=$OPTARG;;
v) VM=$OPTARG;;
p) PYTHON=$OPTARG;;
d) NOTEBOOK_DIR=$OPTARG;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2; echo "$usage" >&2; exit 1;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2; echo "$usage" >&2; exit 1;;
esac
done
# mandatory arguments
if [ ! "$PROJECT" ] || [ ! "$VM" ]; then
echo "arguments -i and -v must be provided"
echo "$usage" >&2; exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment