假设我有一个PHP文件,例如:
<?php
/* Comments */
define('DB_SERVER', 'localhost');
/* More comments */
define('DB_NAME', 'dbname');
define('DB_USER', 'etc');
/* etc...*/
?>
如何仅使用shell脚本来提取DB_SERVER,DB_NAME等的变量值并将它们存储在变量中以在shell脚本中使用? (/ bin / sh,而不是bash)
答案 0 :(得分:5)
# with /bin/bash
. <(awk '/define/ {print $2"="$4}' FS="'" foo.php)
# with /bin/sh
declare `awk '/define/ {print $2"="$4}' FS="'" foo.php`
结果
echo $DB_SERVER # localhost