2017-09-25 119 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

相關問題