我想在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的用戶運行時執行確定。
我試過了,但它對我不起作用。
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`;
不需要\'當用字符串「 – Teson 2012-08-07 06:16:34
@ user247245封裝:您能詳細說明還是提供確切的語法? – Radek 2012-08-07 06:20:28