0

Checking whether the reddis is working using this command

redis-cli ping if output is PONG redis is working.

How to re-write this in shell script like this


#!/bin/bash
redis_metrics=$(redis-cli ping)

if [$redis_metrics == PONG]
then
        echo "100"
else
        echo "0"
fi

Need to assign the echo output value to a variable - redis_status

  • 2
    `if [...]; then redis_status=100; echo "100"; else redis_status=0; echo "0"; fi`?? – David C. Rankin Sep 25 '19 at 04:55
  • 2
    See also [Why should there be a space after '\[' and before '\]' in Bash?](/questions/9581064/why-should-there-be-a-space-after-and-before-in-bash) – tripleee Sep 25 '19 at 05:05
  • Possible duplicate of [Why should there be a space after '\[' and before '\]' in Bash?](https://stackoverflow.com/questions/9581064/why-should-there-be-a-space-after-and-before-in-bash) Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck). – jww Sep 25 '19 at 05:27

1 Answers1

0

You can try this one :

#!/bin/bash
redis_metrics=$(redis-cli ping)

if [ $redis_metrics == PONG ]
then
        redis_status=100
        echo "100"
else
        redis_status=0
        echo "0"
fi
Pacifist
  • 3,025
  • 2
  • 12
  • 20
  • Storing the intermediate result in a variable you never use again is useless, though. Simply `if [ "$(redis-cli ping)" = "PONG" ]; then` (notice also the required spaces inside the `[ ... ]`!)... though if `redis-cli` is at all competently written, it will reflect the result in its status code also; so you can simply say `if redis-cli ping; then` – tripleee Sep 25 '19 at 05:00
  • have an issue if i run it cron it is not working. any inputs. the same if i run in terminal it is working –  Sep 25 '19 at 08:33