I'm creating a basic bash script which deploy a simple webapp. My current code is as follows.#!/bin/bashclearecho ********************************************;echo Hello, I'm going to deploy the QuizProject;echo ********************************************;git pull --all;#Only need to execute if option is presentcomposer install;echo ********************************************;echo All the jobs done! Cheers;echo ********************************************;At the moment I'm running this script in the command asbash deploy.shBut this will execute all the commands in the bash file.I want to make it if specific command is passed then only run the composer installbash -composer deploy.sh
How to check option is set to bash script?
shell script
I understand your question that you want to control the function. Maybe it's best done with options. Here's one way:#!/bin/bashdo_all=1do_git=0do_install=0while getopts gi optdo    case $opt in    (g) do_all=0 ; do_git=1 ;;    (i) do_all=0 ; do_install=1 ;;    (*) printf Illegal option '-%s'\n $opt && exit 1 ;;    esacdoneclearecho ********************************************;echo Hello, I'm going to deploy the QuizProject;echo ********************************************;(( do_all || do_git )) && git pull --all;(( do_all || do_install )) && composer install;echo ********************************************;echo All the jobs done! Cheers;echo ********************************************;If you call that script without options:bash deploy.shboth, git and install, will be called. If you call it with option -i (or resp. -g) only the install (resp. the git call) will be done:bash deploy.sh -ibash deploy.sh -gYou can also specify both options to do both, in one of these ways:bash deploy.sh -gibash deploy.sh -g -i