如何通过在php中传递参数使用exec函数调用bash脚本函数?

时间:2012-03-21 07:14:18

标签: php bash shell magento-1.7

我创建了一个在cpanel中安装magento的bash脚本。但我对exec功能有疑问。

$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");

在exec函数中,我传递了shell脚本创建Magento数据库并安装它所需的所有参数。这是用 function.sh 文件

编写的bash shell脚本代码
#!/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
}`

我的问题是它没有执行脚本的所有命令并直接进入 if condition 的最后一步并打印“请先设置数据库。不要忘记分配数据库用户!“。任何人都可以告诉我的乳清脚本是否正在跳过if部分并直接转到else部分?

1 个答案:

答案 0 :(得分:1)

问题在于我想这一行 "$dbinfo" -eq 'y'

你不能用-eq检查字符串,它只适用于一元语句。
替换为 "$dbinfo" == "y"

它应该工作。