0

Is it possible to call redis-cli in bash?

I tried:

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
        echo 'keys $line' | redis-cli | sed 's/^/get /' | redis-cli
done < "$1"

It returns:

line 4: redis-cli: command not found

I am trying to match the redis key nt:<numbers> as key from input file and display.

Sample input:

nt:60136667431

The command I run:

./updateT.sh input.txt 

Is there any way I can call redis-cli get cmd in script for many key values passed as input?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Ps-kl
  • 97
  • 2
  • 3
  • 14
  • If you don't have it installed then of course you can't. – tripleee Feb 22 '19 at 06:36
  • Looking at some examples, I don't think that's how you use it, though. Probably investigate `xargs` and also see https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – tripleee Feb 22 '19 at 06:38
  • @tripleee i have it installed. redis-cli works in command line but not in bash – Ps-kl Feb 22 '19 at 06:39
  • Bash *is* the command line. At the "command line" where you can run it succesfully, if you type `type -a redis-cli` what do you get? What does your `PATH` look like? – tripleee Feb 22 '19 at 06:40
  • Also, your code doesn't look correct, but we have no idea what you want it to actually do. Could you please [edit] the question to show what you hope those commands would do? – tripleee Feb 22 '19 at 06:42
  • @tripleee yes i get it... i am reading input from a txt file hence m writing the script in a .sh file – Ps-kl Feb 22 '19 at 07:18
  • That doesn't really clarify anything; can you please [edit] your question if you can think of a way to add something actually useful to show us where exactly you are stuck? – tripleee Feb 22 '19 at 07:22

1 Answers1

1

Installation problems aside, I am guessing you want something like

while read -r line; do
    redis-cli keys "$line" |
    xargs -n 1 echo redis-cli get
done <"$1"

Take out the echo if it prints things which look correct to you. If you can get multiple keys, maybe take out the -n 1.

Given the input line nt:60136667431 this will perform

redis-cli keys nt:60136667431

and pass each output line from that to redis-cli get. So if it prints foo and bar we will end up with

echo redis-cli get foo
echo redis-cli get bar

... where again obviously take out the echo if those are the commands you would like to end up executing. If you take out -n 1 it will collect as many as it can into

echo redis-cli get foo bar

though if you have on the order of thousands of results it might still need to split the command line up into multiple commands because of argument list length constraints in the underlying OS. (Google ARG_MAX.) I have no idea whether redis-cli supports this syntax; brief googling suggests not.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I left out the complex `[[ -n "$line" ]]` stuff for simplicity. If you really have files with an incomplete last line, maybe fix them instead. – tripleee Feb 22 '19 at 06:47
  • still got error when i tried this code ./updateTime.sh: line 3: redis-cli: command not found redis-cli get – Ps-kl Feb 22 '19 at 07:29
  • Yeah, you need to solve the installation problem first. – tripleee Feb 22 '19 at 07:34
  • [Let us continue this discussion in chat.](https://chat.stackoverflow.com/rooms/188869/rediis-cli-troubleshooting-between-tripleee-and-ps-kl) – tripleee Feb 22 '19 at 07:38