我正在尝试通过postgres帐户下的psql执行SQK。我可以运行不包含引号的SQL
root@server:/home/rosta/SCRIPTS# su postgres -c "psql -c 'SELECT NOW()'"
now
-------------------------------
2014-06-07 09:38:17.120368+02
(1 row)
使用包含诸如SELECT'hi'的引号的SQL查询会出现问题。我正在用简单的“ hi”进行测试,但是我想从shell脚本中执行类似的操作。
su postgres -c "psql -c 'create database $DB_NAME template=template0 encoding='utf8' owner=aaa lc_collate='cs_CZ.utf8''"
谁能告诉我如何在编码中转义引号并整理上面的命令
我的一些测试:
root@server:/home/rosta/SCRIPTS# su postgres -c "psql -c 'SELECT \'hi\''"
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
root@server:/home/rosta/SCRIPTS# su postgres -c "psql -c 'SELECT \\'hi\\''"
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
root@server:/home/rosta/SCRIPTS# su postgres -c 'psql -c \'SELECT \\'hi\\'\''
我通常要做的是"
对postgres -c
的参数使用双引号(),对的参数使用转义的双引号(\"
)psql -c
。这样,我可以'
在SQL字符串中使用单引号()完全没有问题:
[root@mycomputer ~]# su postgres -c "psql -c \"SELECT 'hi' \" "
?column?
----------
hi
(1 row)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句