これを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をエスケープして実行できるようにするにはどうすればよいですか?
更新I
$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`;