Stan
Stan

Reputation: 189

How do i call bash script function using exec function by passing parameter in php?

I have created a bash script that install magento in a cpanel. but i have a problem regarding the exec function.

$function_path = Mage::getBaseDir()."/media/installer/function.sh"; 
exec("$function_path $db_host $db_name $db_user $db_pass $url $ad_user $ad_pass $ad_email");

In the exec function I am passing all the parameters that the shell script will need to create a Magento databse and install it. This is the bash shell script code written in the function.sh file

#!/bin/bash

magento_detail $dbhost $dbname $dbuser $dbpass $url $admin_username $admin_password $admin_email
function magento_detail()
{

stty erase '^?'

echo "To install Magento, you will need a blank database ready with a user assigned to it."

echo -n "Do you have all of your database information"

dbinfo = "y"
echo $dbinfo 
if [ "$dbinfo" -eq 'y' ]
then
    echo "Database Host (usually localhost) : $dbhost "

    echo "Database Name : $dbname "

    echo "Database User : $dbuser "

    echo "Database Password : $dbpass "

    echo "Store Url : $url "

    echo "Admin Username : $admin_username "

    echo "Admin Password : $admin_password "

    echo "Admin Email Address : $admin_email "

    echo -n "Include Sample Data? (y/n) "

    echo sample = "y"

    if [ "$sample" -eq "y" ];
    then
        echo
        echo "Now installing Magento with sample data..."

        echo
        echo "Downloading packages..."
        echo

        wget http://www.magentocommerce.com/downloads/assets/1.5.1.0/magento-1.5.1.0.tar.gz
        wget http://www.magentocommerce.com/downloads/assets/1.2.0/magento-sample-data-1.2.0.tar.gz

        echo
        echo "Extracting data..."
        echo

        tar -zxvf magento-1.5.1.0.tar.gz
        tar -zxvf magento-sample-data-1.2.0.tar.gz

        echo
        echo "Moving files..."
        echo


        mv magento-sample-data-1.2.0/media/* magento/media/
        mv magento-sample-data-1.2.0/magento_sample_data_for_1.2.0.sql magento/data.sql
        mv magento/index.php magento/.htaccess ./$test1

        echo
        echo "Setting permissions..."
        echo

        chmod o+w var var/.htaccess app/etc
        chmod -R o+w media

        echo
        echo "Importing sample products..."
        echo

        mysql -h $dbhost -u $dbuser -p$dbpass $dbname < data.sql

        echo
        echo "Initializing PEAR registry..."
        echo

         chmod 550 mage

        ./mage mage-setup .

        echo
        echo "Downloading packages..."
        echo

        echo
        echo "Cleaning up files..."
        echo

        rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*
        rm -rf magento/ magento-sample-data-1.2.0/
        rm -rf magento-1.5.1.0.tar.gz magento-sample-data-1.2.0.tar.gz data.sql
        rm -rf index.php.sample .htaccess.sample php.ini.sample LICENSE.txt STATUS.txt data.sql

        echo
        echo "Installing Magento..."
        echo

        php -f install.php --license_agreement_accepted "yes" --locale "en_US" --timezone "America/Los_Angeles"    --default_currency "USD" --db_host "$dbhost" --db_name "$dbname" --db_user "$dbuser" --db_pass "$dbpass" --url "$url"            --use_rewrites "yes" --use_secure "no" --secure_base_url "" --use_secure_admin "no" --admin_email "$admin_email"        --admin_username "$admin_username" --admin_password "$admin_password"

        echo
        echo "Finished installing Magento"
        echo

        exit
    else
        echo "Now installing Magento without sample data..."

        echo
        echo "Downloading packages..."
        echo

        wget http://www.magentocommerce.com/downloads/assets/1.5.1.0/magento-1.5.1.0.tar.gz

        echo
        echo "Extracting data..."
        echo

        tar -zxvf magento-1.5.1.0.tar.gz

        echo
        echo "Moving files..."
        echo

        mv magento/* magento/.htaccess .

        echo
        echo "Setting permissions..."
        echo

        chmod o+w var var/.htaccess app/etc
        chmod -R o+w media

        echo
        echo "Initializing PEAR registry..."
        echo

        chmod 550 mage

        ./mage mage-setup .

        echo
        echo "Downloading packages..."
        echo

        echo
        echo "Cleaning up files..."
        echo

        rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*
        rm -rf magento/ magento-1.5.1.0.tar.gz
        rm -rf index.php.sample .htaccess.sample php.ini.sample LICENSE.txt STATUS.txt

        echo
        echo "Installing Magento..."
        echo

        php -f install.php --license_agreement_accepted "yes" --locale "en_US" --timezone "America/Los_Angeles"          --default_currency "USD" --db_host "$dbhost" --db_name "$dbname" --db_user "$dbuser" --db_pass "$dbpass" --url "$url"            --use_rewrites "yes" --use_secure "no" --secure_base_url "" --use_secure_admin "no" --admin_email "$admin_email"            --admin_username "$admin_username" --admin_password "$admin_password"

        echo
        echo "Finished installing Magento else part"

        exit
    fi
else
    echo "Please setup a database first. Don't forget to assign a database user!"

    exit
fi
}`

My problem is that it doesn't execute all commands of the script and directly goes to the last step of if condition and prints "Please setup a database first. Don't forget to assign a database user!". Can anyone tell my whey the script is skipping the if part and directly going to the else part?

Upvotes: 0

Views: 755

Answers (1)

Roi Rav-Hon
Roi Rav-Hon

Reputation: 91

The problem is with this line i suppose
"$dbinfo" -eq 'y'

you can't check strings with -eq, it is just for unary statements.
replace with
"$dbinfo" == "y"

and it should work.

Upvotes: 1

Related Questions