我想在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