2017-09-25 183 views
0

寻找能够自动化bash脚本获取.PHP程序内容的能力,并在特定的目录中以755的权限创建它。我基本上想要为用户提供这一个.sh脚本,该脚本将安装相应的程序和文件以启动并运行网站。我遇到的问题是PHP变量不会保存在输出文件中。我正在使用以下命令:创建一个bash shell脚本,可以创建一个PHP程序

echo "<?php 
header('Content-Type: text/xml'); 
require_once '/var/www/osbs/PHPAPI/account.php'; 
require_once '/var/www/osbs/zang/library/Zang.php'; 
$To = $_POST['subject']; 
$Body = $_POST['text']; 
# If you want the response decoded into an Array instead of an Object, set 
response_to_array to TRUE, otherwise, leave it as-is 
$response_to_array = false; 
# Now what we need to do is instantiate the library and set the required 
options defined above 
$zang = Zang::getInstance(); 
# This is the best approach to setting multiple options recursively Take note that you cannot set non-existing options 
$zang -> setOptions(array(
'account_sid' => $account_sid, 
'auth_token' => $auth_token, 
'response_to_array' => $response_to_array)); 
?>" | tee /var/www/output.php 

output.php文件缺少所有以$开头的变量,你们可以帮忙吗?

+0

当然,你不会真的想将一些PHP代码硬编码到你的bash脚本中吗?它不应该只是复制文件,并可能创建一个数据库? – ADyson

+0

您需要在bash脚本中使用反斜杠'\ $' –

+0

来跳过'$'或在bash脚本中使用单引号围绕您的php代码。 –

回答

1

在这里处理报价问题的最简单方法是使用"here-doc"

cat >/var/www/output.php <<"EOF" 
<?php 
header('Content-Type: text/xml'); 
require_once '/var/www/osbs/PHPAPI/account.php'; 
require_once '/var/www/osbs/zang/library/Zang.php'; 
$To = $_POST['subject']; 
$Body = $_POST['text']; 
# If you want the response decoded into an Array instead of an Object, 
# set response_to_array to TRUE, otherwise, leave it as-is 
$response_to_array = false; 
# Now what we need to do is instantiate the library and set the 
# required options defined above 
$zang = Zang::getInstance(); 
# This is the best approach to setting multiple options recursively. 
# Take note that you cannot set non-existing options 
$zang -> setOptions(array(
'account_sid' => $account_sid, 
'auth_token' => $auth_token, 
'response_to_array' => $response_to_array)); 
?> 
EOF 

没有必要tee(除非你真的想转储所有的东西到控制台,这似乎是不必要的) 。引用分隔符字符串(<<"EOF")有效地引用整个here-doc,防止变量的扩展。

+0

谢谢!像魅力一样工作 – fixnode