我尝试使用bash为我的网站生成一个conf文件:./ conf.sh dbu2 dbn2 pass2
我的bash scrypt :
#!/bin/bash
dbuser=$1
dbname=$2
dppass=$3
cat <<EOF > /var/www/mydomain.com/public_html/config.php
<?php
$DB='mysql';
$DBhost='localhost';
$DBname='$2';
$DBusr='$1';
$DBpwd='$3';
(...)
?>
EOF
输出(/var/www/mydomain.com/public_html/config.php):
<?php
='mysql';
='localhost';
='dbn2';
='dbu2';
='pass2';
应该是:
<?php
$DB='mysql';
$DBhost='localhost';
$DBname='dbn2';
$DBusr='dbu2';
$DBpwd='pass2';
(...)
?>
有任何建议吗?
谢谢大家:)
答案 0 :(得分:2)
您需要转义要保留的变量名中的$
(以防止变量扩展):
cat <<EOF > /var/www/mydomain.com/public_html/config.php
<?php
\$DB='mysql';
\$DBhost='localhost';
\$DBname='$2';
\$DBusr='$1';
\$DBpwd='$3';
(...)
?>
EOF