When developing a site to automate the deployment process, I wrote several bash scripts.
To customize the behavior of scripts, I use the transfer of arguments, which are handled in the bash script by the getopts library.
Getopts
Scripts typically use getopts to analyze the arguments passed to them. When you specify args on the command line, getopts analyzes these arguments instead of a script, and then the script uses the parsed data that getopts parsed.
For example, in my case I use several actions that must be performed by the script.
while getopts p:dicmz option do case "${option}" in p) PASSWORD=${OPTARG};; # Password for reload services: gunicorn and celery d) DEPLOY_PROJECT_PACKAGES=true;; # Deploy local projects packages i) INSTALL_REQUIREMENTS=true;; # Install or update new packages from requirements c) COLLECTSTATIC=true;; # Execute collectstatic m) MIGRATE=true;; # Execute migrate z) INGORE_BACKUP=true;; # WARNING: Be careful with this option. # Do not use it, when execute migrations or add new application or git submodule esac done
I am running a project on the site, installing or updating required packages in a project, building a new statics, migrating a database and, if necessary, ignoring the creation of a project backup, although I rarely use this. It is better to be safe once again and perform a backup.
Thus, the script can be run with parameters in this way.
./deploy.sh -icm -p mypassword
In this case, a password will be transferred to perform specific functions requiring a password, and installation or updating of required packages, static build and database migration will be performed.
The passed parameter not requiring an argument will be checked in the conditions so
if [ "$INGORE_BACKUP" ] then # Deployment without backup" else # Execute backup fi