1

これを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>&1postgresユーザーとして実行している場合は正常に実行されます。

試しましたが、うまくいきません。

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`;
4

2 に答える 2

2

この関数を調べることができます:

http://php.net/manual/en/function.escapeshellcmd.php

于 2012-08-07T06:15:17.497 に答える
1

シェルにコマンドを直接実行するためにphpで使用されるバッククォート`を使用できます

元:

$output = `ls -l`;

あなたの場合、それは次のようになります:

$shellOutput=`sudo -u postgres psql -c "alter user edumate with encrypted password 'BLAH_BLAH';" template1 2>&1`;
于 2012-08-07T06:20:50.653 に答える