0

For example, i have a key list in file named keys, and some key name contains blank:

keya
keyb
"a blank key"

I want to get values from redis in bash, so my command is:

while read key ;do redis-cli -h 127.0.0.1 get $key;done < keys

But when the name contains blank, the comand executes failed:

none
none
(error) ERR wrong number of arguments for 'type' command

Does anynoe knows how to fix this? Thanks very much.

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
mark.gao
  • 27
  • 3
  • Test for an empty variable, `while ...; do [[ -z "$key" ]] && continue ; redis ...; done` – Jetchisel Sep 24 '22 at 13:13
  • or instead of `< keys` use `< <(grep -v '^$' keys)` – Jetchisel Sep 24 '22 at 13:15
  • Having quotes inside the key variable doesn't substitute for not using them around `$key`, because the quotes from your data file are _literal_, quotes inside your source code are _syntax_, and the two don't substitute for each other. Take them out of `"a blank key"` -- the line should be just `a blank key` -- and then make it `get "$key"`, not `get $key`. – Charles Duffy Sep 24 '22 at 13:37
  • BTW, you'd have the same problem for _any_ key with spaces in it, not just ones where those spaces surround the word "blank". (Empty keys are also a problem when you don't use syntactic quotes at expansion time, because they don't turn into any arguments at all). – Charles Duffy Sep 24 '22 at 13:40
  • Thanks all of you. @Charles Duffy's way works. – mark.gao Sep 24 '22 at 15:28

0 Answers0