phpdirectadmin

Bash script skip first code and run second if option disabled


I have available 2 packages with name:

Currently I have bash script set for one package:

if [[ $package1 == 'OFF' ]]; then
  exit 0;
else
.......
......
......
do something
......
.....
.....
EOF
 chown $username. /home/$username/domains/$domain/public_html/.htaccess
 fi

Now when this package is OFF then we this script exit. Correct.

ok, but I want to run the code now depending on which option is enabled.

I try somelike this:

### START THIS CODE RUN ONLY WHEN PACKAKGE1 IS ON 
    if [[ $package1 == 'ON' ]]; then
        .......
        ......
        ......
        do something
        ......
        .....
        .....
        EOF
         chown $username. /home/$username/domains/$domain/public_html/.htaccess
         fi
### END

### START THIS CODE RUN ONLY WHEN PACKAKGE2 IS ON 
    elseif [[ $package2 == 'ON' ]]; then
        .......
        ......
        ......
        do something
        ......
        .....
        .....
        EOF
         chown $username. /home/$username/domains/$domain/public_html/.htaccess
         fi

But I guess I'm doing something wrong. Can someone please help me to do this. If the given option is enabled, it only wants to execute the code for the given option and skip the other codes.


Solution

  • When pkg 1 and 2 are ON, only 1 will executed:

    if [ "$package1" = "ON" ]; then
        chown...
    elif [ "$package2" = "ON" ]; then
        chown...
    fi
    

    When pkg 1 and 2 are ON, both will executed:

    if [ "$package1" = "ON" ]; then
        chown...
    fi
    if [ "$package2" = "ON" ]; then
        chown...
    fi