我需要帮助传递 Linux bash 脚本变量来执行 PHP CLI 命令。
我正在开发执行 PHP CLI 命令的 bash 脚本,该命令从 bash 脚本(如文件夹路径)获取输入变量,以包含用于访问 PHP 文件类的文件。
但是执行 bash 脚本时发生的情况是,传递给 PHP CLI 命令的变量充当 PHP 的变量。
下面是我的脚本的示例代码
file="$folderpath"somefile.php
mage_version=$(php -r 'include "$file";print_r(Mage::getVersionInfo());')
下面是我收到的错误
PHP Notice: Undefined variable: file in Command line code on line 1
PHP Warning: include(): Filename cannot be empty in Command line code on line 1
最佳答案
Bash 需要双引号 ("
) 来解析变量,否则它们将保留 $var
,因此 php 会读取它们;
file="$folderpath"somefile.php
mage_version=$(php -r "include \"${file}\";print_r(Mage::getVersionInfo());")
如果您需要多线解决方案,您可以执行以下操作:Run PHP function inside Bash (and keep the return in a bash variable)
php_cwd=`/usr/bin/php << 'EOF'
<?php echo getcwd(); ?>
EOF`
echo "$php_cwd" # Or do something else with it
关于php - 如何在 PHP 命令 (CLI) 中传递 bash 脚本变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57971340/