2

This question already has an answer here:

I'm creating a basic bash script which deploy a simple webapp. My current code is as follows.

#!/bin/bash
clear

echo "********************************************";
echo "Hello, I'm going to deploy the QuizProject";
echo "********************************************";

git pull --all;

#Only need to execute if option is present
composer install;

echo "********************************************";
echo "All the jobs done! Cheers";
echo "********************************************";

At the moment I'm running this script in the command as

bash deploy.sh

But 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 install"

bash -composer deploy.sh

marked as duplicate by jasonwryan, cuonglm, Anthon, Stephen Kitt, X Tian Jun 4 '15 at 8:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I don't want to validate any file name. Only needed to parameter set or not – Achintha Samindika Jun 4 '15 at 7:03
2

I understand your question that you want to control the function. Maybe it's best done with options. Here's one way:

#!/bin/bash

do_all=1
do_git=0
do_install=0
while getopts "gi" opt
do
    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 ;;
    esac
done

clear

echo "********************************************";
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.sh

both, 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 -i
bash deploy.sh -g

You can also specify both options to do both, in one of these ways:

bash deploy.sh -gi
bash deploy.sh -g -i
2
#!/bin/bash
clear

echo "********************************************";
echo "Hello, I'm going to deploy the QuizProject";
echo "********************************************";

git pull --all;

#Only need to execute if option is present
if test "$1" = "-composer"; then
    composer install;
fi

echo "********************************************";
echo "All the jobs done! Cheers";
echo "********************************************";

Run the script as "bash deploy.sh -composer" to do the composer part.

Not the answer you're looking for? Browse other questions tagged or ask your own question.