簡體   English   中英

我需要兩次使用單引號 - 如何逃避?

[英]I need to use single quotes twice - how to escape?

我想在PHP中運行它但由於引號它不起作用....

    $cmd="sudo -u postgres sh -c 'psql -c \"alter user edumate with encrypted password \'BLAH_BLAH\';\" template1 2>&1' ";
    $shellOutput = exec($cmd, $output);

psql -c "alter user edumate with encrypted password 'BLAH_BLAH';" template1 2>&1 當作為postgres用戶運行時, psql -c "alter user edumate with encrypted password 'BLAH_BLAH';" template1 2>&1執行正常。

我試過但它對我不起作用。

sudo -u postgres sh -c 'psql -c \"alter user edumate with encrypted password \'BLAH_BLAH\';\" template1 2>&1'
sudo -u postgres sh -c 'psql -c "alter user edumate with encrypted password \'BLAH_BLAH\';" template1 2>&1'

我怎么能逃脫$ cmd所以我可以執行它?

更新我

    $subcommand="alter user edumate with encrypted password 'BLAH_BLAH';";
    $sub = escapeshellarg($subcommand);
    $cmd="sudo -u postgres sh -c 'psql -c \"".$sub."\" template1 2>&1'";
    $shellOutput = exec($cmd, $output);
    echo "<pre>Output = " . print_r($output,1) . "</pre>";

回報

Output = Array
(
)

更新II

感謝@Hawili的工作代碼。 我想知道如何用sh -c'命令'運行,他省略了。

$shellOutput=`sudo -u postgres psql -c "alter user edumate with encrypted password 'BLAH_BLAH';" template1 2>&1`;

您可以使用php使用的back quote來直接執行命令到shell

例如:

$output = `ls -l`;

在你的情況下,它可以是這樣的:

$shellOutput=`sudo -u postgres psql -c "alter user edumate with encrypted password 'BLAH_BLAH';" template1 2>&1`;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM