如何使用保留字转义 "su -c 'psql ... -c \"\"' "
How to escape a "su -c 'psql ... -c \"\"' " with reserved words
保留字'user'对我来说是个问题,因为它应该用""(双引号)转义,但要用不同引号的组合('和"和\'和"/)psql作为用户传递它(没有双重转义)。如何转义这个特殊的词?
我已经尝试过使用 $$(美元引用的字符串常量)或将它放在不同的变量中,但没有任何帮助。
DB=mydb
MAIL_USER=my@mail.de
MAIL_PASS=password
MAIL_SERVER=mail.server.de
MAIL_PORT_IMAP=993
MAIL_PORT_SMTP=465
su -c "psql -d $DB -c \"INSERT INTO fetchmail_server(name, \"user\", type, server, port, is_ssl, password, active, priority, state, original) SELECT '$MAIL_USER', '$MAIL_USER', 'imap', '$MAIL_SERVER', $MAIL_PORT_IMAP, True, '$MAIL_PASS', True, 1, 'done', True WHERE NOT EXISTS (SELECT 1 FROM fetchmail_server WHERE name='$MAIL_USER')\"" postgres
预期结果是,该命令在数据库中执行
INSERT INTO fetchmail_server(name, "user", type, server, port, is_ssl, password, active, priority, state, original) SELECT 'my@mail.de', 'my@mail.de', 'imap', 'mail.server.de', 993, True, 'password', True, 1, 'done', True WHERE NOT EXISTS (SELECT 1 FROM fetchmail_server WHERE name='my@mail.de')
谢谢@wildplasser,您对正确用户使用 psql 的提示是正确的。所以我能够减少一个嵌套级别。有效:
SQL_COMMAND='INSERT INTO fetchmail_server(name, "user", type, server, port, is_ssl, password, active, priority, state, "original") SELECT '\'$MAIL_USER\'', '\'$MAIL_USER\'', '\'imap\'', '\'$MAIL_SERVER\'', '\'$MAIL_PORT_IMAP\'', True, '\'$MAIL_PASS\'', True, 1, '\'done\'', True WHERE NOT EXISTS (SELECT 1 FROM fetchmail_server WHERE name='\'$MAIL_USER\'')'
echo $SQL_COMMAND
psql -d $DB -U postgres -c "$SQL_COMMAND"
保留字'user'对我来说是个问题,因为它应该用""(双引号)转义,但要用不同引号的组合('和"和\'和"/)psql作为用户传递它(没有双重转义)。如何转义这个特殊的词?
我已经尝试过使用 $$(美元引用的字符串常量)或将它放在不同的变量中,但没有任何帮助。
DB=mydb
MAIL_USER=my@mail.de
MAIL_PASS=password
MAIL_SERVER=mail.server.de
MAIL_PORT_IMAP=993
MAIL_PORT_SMTP=465
su -c "psql -d $DB -c \"INSERT INTO fetchmail_server(name, \"user\", type, server, port, is_ssl, password, active, priority, state, original) SELECT '$MAIL_USER', '$MAIL_USER', 'imap', '$MAIL_SERVER', $MAIL_PORT_IMAP, True, '$MAIL_PASS', True, 1, 'done', True WHERE NOT EXISTS (SELECT 1 FROM fetchmail_server WHERE name='$MAIL_USER')\"" postgres
预期结果是,该命令在数据库中执行
INSERT INTO fetchmail_server(name, "user", type, server, port, is_ssl, password, active, priority, state, original) SELECT 'my@mail.de', 'my@mail.de', 'imap', 'mail.server.de', 993, True, 'password', True, 1, 'done', True WHERE NOT EXISTS (SELECT 1 FROM fetchmail_server WHERE name='my@mail.de')
谢谢@wildplasser,您对正确用户使用 psql 的提示是正确的。所以我能够减少一个嵌套级别。有效:
SQL_COMMAND='INSERT INTO fetchmail_server(name, "user", type, server, port, is_ssl, password, active, priority, state, "original") SELECT '\'$MAIL_USER\'', '\'$MAIL_USER\'', '\'imap\'', '\'$MAIL_SERVER\'', '\'$MAIL_PORT_IMAP\'', True, '\'$MAIL_PASS\'', True, 1, '\'done\'', True WHERE NOT EXISTS (SELECT 1 FROM fetchmail_server WHERE name='\'$MAIL_USER\'')'
echo $SQL_COMMAND
psql -d $DB -U postgres -c "$SQL_COMMAND"