简体   繁体   English

如何在 sql 查询 linux shell 中使用“%”字符?

[英]How to use "%" character in sql query on linux shell?

I am trying to pull all the jdk packages installed on set of hosts by sending a sql select statement to osquery on linux shell via pssh.我试图通过 pssh 向 linux shell 上的 osquery 发送 sql select 语句来拉取安装在一组主机上的所有 jdk 包。

Here is the query:这是查询:

pssh -h myhosts -i 'echo "SELECT name FROM rpm_packages where name like '%jdk%';"| osqueryi --json'

but usage of "%" is giving me below error.但是使用“%”给我以下错误。

Error: near line 1: near "%": syntax error

I tried to escape %,but the error remains same.我试图转义 %,但错误仍然存在。 Any ideas how to overcome this error?任何想法如何克服这个错误?

You aren't getting this error from your shell but from the query parser, and it's not actually caused by the % character, but to the ' that immediately precedes it.您不是从 shell 中收到此错误,而是从查询解析器中收到此错误,它实际上不是由%字符引起的,而是由它前面的'引起的。 Look at where you have quotes:看看你在哪里引用:

'echo "SELECT name FROM rpm_packages where name like '%jdk%';"| osqueryi --json'
^----------------------------------------------------^     ^-------------------^

These quotes are consumed by the shell when it parses the argument. shell 在解析参数时使用这些引号。 Single quotes tell the shell to ignore any otherwise-special characters inside and treat what is within the quotes as part of the argument -- but not the quotes themselves.单引号告诉 shell 忽略里面的任何其他特殊字符,并将引号的内容视为参数的一部分——而不是引号本身。

After shell parsing finishes, the actual, verbatim argument that gets sent to pssh looks like this: shell 解析完成后,发送到pssh的实际逐字参数如下所示:

echo "SELECT name FROM rpm_packages where name like %jdk%;"| osqueryi --json

Note that all of the single quotes have been erased.请注意,所有单引号都已被删除。 The result is that your query tool sees the % (presumably modulus) operator in a place that it doesn't expect -- right after another operator ( like ) which makes about as much sense to the parser as name like * jdk .结果是您的查询工具在它不期望的地方看到了% (可能是模数)运算符——就在另一个运算符( like )之后,这对解析器来说与name like * jdk有意义。 The parser doesn't understand what it means to have two consecutive binary operators, so it complains about the second one: % .解析器不理解有两个连续的二元运算符意味着什么,所以它抱怨第二个: %

In order to get a literal ' there, you need to jump through this hoop:为了在那里获得文字' ,您需要跳过这个圈:

'\''
^^^^- start quoting again
|||
|\+-- literal '
|
\---- stop quoting

So, to fix this, replace all ' instances inside the string with '\'' :因此,要解决此问题,请将字符串中的所有'实例替换为'\''

pssh -h myhosts -i 'echo "SELECT name FROM rpm_packages where name like '\''%jdk%'\'';"| osqueryi --json'

osqueryi accepts a single statement on the command line. osqueryi 接受命令行上的单个语句。 Eliminating the echo can make quoting a bit simpler:消除回声可以使引用更简单一些:

osqueryi --json "SELECT * FROM users where username like '%jdk%'"

You will, however, need the quotes to pass through your pssh command line.但是,您需要引号才能通过pssh命令行。

While osqueryi is great for short simple things, if you're building a frequent polling service, osqueryd with scheduled queries is generally simpler.虽然osqueryi非常适合简短的事情,但如果您要构建频繁的轮询服务,则带有计划查询的osqueryd通常更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM