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.

5
  • Test for an empty variable, while ...; do [[ -z "$key" ]] && continue ; redis ...; done Commented Sep 24, 2022 at 13:13
  • or instead of < keys use < <(grep -v '^$' keys) Commented Sep 24, 2022 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. Commented Sep 24, 2022 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). Commented Sep 24, 2022 at 13:40
  • Thanks all of you. @Charles Duffy's way works. Commented Sep 24, 2022 at 15:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.