Question

Can an if statement in a bash script confirm its current folder based on wildcards?

I am making a script to quickly place a drupal site in maintenance mode, ask if you want to keep the .htaccess file, keep the sites folder and the .htaccess file (if requested), update modules and the database then take the site out of maintenance mode. I have accomplished the above, but for ease of use, I would like to have one master script for all site folders, stored in a central location.

Here is my script that works as designed:

#/bin/bash
CWD=$(pwd)
cd $CWD
echo $CWD
if [ $CWD = "/var/www/vhosts/specific.site.folder" ]; then
    echo "Updating drupal core files"
    read -r -p "do you need to keep the .htaccess file? [y/N]" response

if [ $response = y ] 
then
    /usr/local/bin/drush vset maintenance_mode 1
    mv ./.htaccess ../.htaccess
    mv ./sites ../sites
    rm -rf *
    cp -R /sources/drupal/* ./
    mv ../sites ./sites
    mv ../.htaccess ./.htaccess
    chown -R httpd:httpd *
    /usr/local/bin/drush up && /usr/local/bin/drush updb
    /usr/local/bin/drush vset maintenance_mode 0

elif [ $response = n ] 
then
    /usr/local/bin/drush vset maintenance_mode 1
    mv ./sites ../sites
    rm -rf *
    cp -R /sources/drupal/* ./
    mv ../sites ./sites
    chown -R httpd:httpd *
    /usr/local/bin/drush up && /usr/local/bin/drush updb
    /usr/local/bin/drush vset maintenance_mode 0
else
    echo "Response must be either y or n"
fi
else
        echo "not in a web directory, exiting"
fi

What I would like is for the same script, but something like:

if [ $CWD = "/var/www/vhosts/*" ]; then

This did not work. I would like this to work so that the same script can run for any site, but keep other directories safe, including subdirectories on the site folder itself.

If there are better ways to do anything I have there, please suggest them.

the servers that this is intended to run on are based on centos 6, but it may need to be used on a debian based machine at some point, if anything needs changing please also let me know.

Was it helpful?

Solution

First, you need to use the [[ command; [ does not do pattern matching. Second, the asterisk needs to be unquoted to be treated as a pattern metacharacter, rather than a literal asterisk.

if [[ $CWD = "/var/www/vhosts/"* ]]; then
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top