在shell中转义postgresql的单引号 [英] Escaping single quotes in shell for postgresql
问题描述
不包含引号
root @ server:/ home / rosta / SCRIPTS#su postgres - cpsql -c'SELECT NOW()'
now
----------------------------- -
2014-06-07 09:38:17.120368 + 02
(1行)
出现的问题出现在sql查询中,包含诸如SELECT'hi'的引号。我用简单的'嗨'测试
,但是我想从shell脚本执行这样的事情。
su postgres -cpsql -c'create database $ DB_NAME template = template0 encoding ='utf8'owner = aaa lc_collate ='cs_CZ.utf8''
任何人都可以建议我如何绕过编码排除引号,并在上面的命令中排序
谢谢
Rosta
我的一些测试
root @ server:/ home / rosta / SCRIPTS#su postgres -cpsql -c'SELECT \'hi\''
bash:-c:行0:意外的EOF,同时寻找匹配 '
bash:-c:行1:语法错误:文件的意外结束
root @ server:/ home / rosta / SCRIPTS#su postgres -cpsql -c'SELECT \\'
bash:-c:行0:意外的EOF寻找匹配``'
bash:-c:行1:语法错误:文件的意外结束
root @ server:/ home / rosta / SCRIPTS# su postgres -c'psql -c \'SELECT \\'hi\\\\\''
我通常做的是使用双引号()for
postgres -c
的参数和转义双引号( \
)for psql -c
的论点。这样,我可以在SQL字符串中使用单引号('
),没有问题:
[root @ mycomputer〜]#su postgres -cpsql -c \SELECT'hi'\
?column?
----------
hi
(1行)
Im trying execute sql through psql under postgres account. Im able run sql that doesnt contain quotes
root@server:/home/rosta/SCRIPTS# su postgres -c "psql -c 'SELECT NOW()'"
now
-------------------------------
2014-06-07 09:38:17.120368+02
(1 row)
Problem appears with sql query that contains quotes like SELECT 'hi'. Im testing with simple 'hi', but I would like execute something like this from shell script.
su postgres -c "psql -c 'create database $DB_NAME template=template0 encoding='utf8' owner=aaa lc_collate='cs_CZ.utf8''"
Can anyone advise me how to escape quotes around encoding and collate in command above
Thanks
Rosta
Some of my test
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\\'\''
What I usually do is use double quotes ("
) for postgres -c
's argument and escaped double quotes (\"
) for psql -c
's argument. That way, I can use single quotes ('
) inside the SQL string with no problem:
[root@mycomputer ~]# su postgres -c "psql -c \"SELECT 'hi' \" "
?column?
----------
hi
(1 row)
这篇关于在shell中转义postgresql的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!