2014-05-21 36 views
0

不知道如何正確描述我想要的內容,但在大量使用Google搜索後,我需要一些幫助。

我使用PHP來運行一些shell命令,其中一個shell命令使用字符串創建一個包含其中的一些配置信息的文件。這個配置信息有一個字符串「$ uri」的部分,所以顯然如果我把它用雙引號括起來「它試圖調用不存在的變量。我的問題是當我用單引號稱它忽略$ URI字完全...這是正常的行爲

我會包括一個例子,我正在寫一個nginx的serverblock文件,以便它如下:

$nginxhost = 'server { 
    listen 80; 

    root /var/www/dev2.host.dev/public_html; 
    index index.php index.html index.htm; 

    # Make site accessible from http://localhost/ 
    server_name dev2.host.dev; 
    include hhvm.conf; 

    location/{ 
     # First attempt to serve request as file, then 
     # as directory, then fall back to displaying a 404. 
     try_files $uri $uri/ =404; 
     # Uncomment to enable naxsi on this location 
     # include /etc/nginx/naxsi.rules 
    } 
}'; 

然後通過SSH2執行:

"echo \"$passsowrd\" | sudo -S sh -c 'echo \"".$nginxhost."\" >> /etc/nginx/sites-available/dev2.host.dev';"; 

我應該用{}?包裝整個$ nginxhost嗎? $ uri?

+1

能否請您包括最少的編碼的例子嗎?我們可以重現問題的一個? – GolezTrol

+0

試試'$ param =「{$ uri}」;' – jp2code

+0

用示例更新 – user1943442

回答

1

對於特殊字符(可以理解爲語言的語法),你應該使用反斜槓\\$uri

相關問題