0

I need to issue this command via sudo in php. But sudo needs to have the command inside apostrophes.

psql -c "create database radek with encoding 'unicode';" -U edumate template1

so

sudo -su postgres 'psql -c "create database radek with encoding ''unicode'';" -U edumate template1'

gives me an error

ERROR:  syntax error at or near "unicode"
LINE 1: create database radek with encoding unicode;
                                            ^

I'd say that the error happens because unicode is enclosed by '. And sudo command is enclosed by ' too.

UPDATE

escaping unicode \'unicode\' doesn't work. I get > and it hangs there for ever....

UPDATE2

the final php code is like

exec("sudo -su postgres 'psql -c \"create database " . $db . " with encoding '\"'\"'unicode'\"'\"';\" -U edumate template1'", $output); 

thanks to @Matthew Scharley

4

1 回答 1

1

这有点奇怪,但是你的双撇号发生的事情是它关闭了字符串然后再次重新打开它,这让你一无所有。解决方案如下:

sudo -su postgres 'psql -c "create database radek with encoding '"'"'unicode'"'"';" -U edumate template1'

IE。将撇号放在收盘价和开盘价之间的双引号中。这至少可以在bashtype shell 中工作,但我不使用其他人来了解它们是如何工作的。

于 2011-03-29T00:52:11.227 回答