[英]Why doesn't this xargs work?
I've been trying to get all of the value within a redis db, using redis-cli. 我一直在尝试使用redis-cli在redis db中获取所有值。 This is done in principal by redis-cli keys "*"
(returns all keys) and piping that to redis-cli get
(returns the value of a key). 原则上,这是通过redis-cli keys "*"
(返回所有键)和管道传递给redis-cli get
(返回键的值)来完成的。
I've since discovered (on SO) a way to do it: 从那以后,我发现了一种方法:
echo 'keys YOURKEY*' | redis-cli | sed 's/^/get /' | redis-cli
But before, I was trying this, and the get command never seemed to run. 但是在此之前,我尝试过此操作,并且get命令似乎从未运行过。 Am I doing xargs wrong?: 我做错了xargs吗?:
redis-cli keys "*" | xargs -0 -I % redis-cli get "%"
I appreciate that 'why didn't this work' questions are frowned upon, but I think the answer will be informative for people confused by xargs. 我很欣赏“为什么不做这项工作”的问题,但我认为答案对于xargs感到困惑的人将是有益的。
You're using the -0
flag to xargs
, which says that your input items are terminated by a null character (ASCII 0) rather than whitespace. 您正在对xargs
使用-0
标志,该标志表示您的输入项以空字符(ASCII 0)而不是空格终止。 It is unlikely that the redis-cli
command outputs keys in this format. redis-cli
命令不太可能以这种格式输出密钥。 In fact, it seems to produce output that has one key/line, like this: 实际上,似乎产生的输出只有一个键/行,如下所示:
# redis-cli keys "*"
_tooz_group:central-global
_tooz_beats:38225c46-7ed8-4c2a-b1eb-6400d0f99004
_tooz_beats:bea903f7-ab5d-4503-be19-f2029beece93
_tooz_beats:bd859c91-0245-45cf-a289-23fc25998e97
_tooz_beats:04528af5-dd97-41af-87a6-d7dc0c0d9f5d
_tooz_beats:974b9d9d-86ff-457e-9a5c-e857ec12a915
_tooz_beats:a55cfe65-f344-4ed4-9b9f-a0ace4d8f6d3
If you drop the -0
from your xargs
invocation, it should work fine: 如果从xargs
调用中删除-0
,它应该可以正常工作:
# redis-cli keys "*" | xargs -I% redis-cli get %
If you have keys containing whitespace, this won't work (because xargs
expects your input data to be whitespace delimited). 如果您有包含空格的键,则此键将不起作用(因为xargs
期望输入数据以空格分隔)。 In that case, you can explicitly set the delimiter to \\n
: 在这种情况下,您可以将分隔符显式设置为\\n
:
# redis-cli keys "*" | xargs --delimiter '\n' -I% redis-cli get %
I think you want this: 我想你想要这个:
# Set some data
redis-cli set mb1 A
OK
# Set some more
redis-cli set mb2 B
OK
# Check the keys we have
redis-cli keys "mb*"
1) "mb1"
2) "mb2"
#Retrieve those puppies, passing one at a time with "-n 1"
redis-cli keys "mb*" | xargs -n 1 redis-cli get
"A"
"B"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.