2015-02-06 36 views

回答

1

你似乎試圖在單引號內包裝單引號,這當然是你不能做的。 'this'and'that'解析爲'this',然後是未加引號的and,然後是'that',而不是引用字符串this'and'that

通常的解決方案是用雙引號將單引號括起來,反之亦然,所以"this'and'that"'this"and"that'如果用引號內的雙引號替換引號是可以接受的;但在這裏,你已經擁有了兩個,所以你不能那麼做(直截了當地)。

假設psql可以從stdin讀取命令,這裏的簡單解決方法是使用here document

su -c "psql -d myDB-c <<'____HERE' 
    SELECT count(*) AS number, 
      date_trunc('day'::text, users.registerdate) AS registerdate 
    FROM users; 
____HERE" postgres 
+0

您也可以使用Bash的「ANSI C」字符串語法。見http://stackoverflow.com/questions/11966312/how-the-leading-dollar-sign-affects-single-quotes-in-bash – tripleee 2015-02-06 10:28:23