1

I am trying to create a simple bash script. (just started working on bash scripting) the script is simple. There is an issue with rsyslog service and from time to time dies. I am trying to make a script to check if service is dead to restart it till now I want to check if my conditions are ok but it seems I am getting an error. Can you advice me ?

Here is the script:

#!/bin/bash
a="dead"
b="running"
while i in $(/etc/init.d/rsyslog status | grep -o 'running\|dead');
do
if
[ "$a" == "$i" ];
then
echo "service rsyslog is dead "
if
[ "$b" == "$i" ];
then
echo "service rsyslog is running"
else
echo "nothing to do";
fi;
done
-------------

I am getting the following syntax error.

./rsyslogcheck.sh: line 17: syntax error near unexpected token done' ./rsyslogcheck.sh: line 17:done'

Thank you in advance!!

3
  • 5
    Run this through shellcheck.net - the first if is missing its fi. Commented Dec 20, 2016 at 14:50
  • You don't need to use both a semicolon (;) and a newline to separate statements / commands. Commented Dec 20, 2016 at 14:50
  • You are also confusing while loops with for loops. There is no in operator in bash; in is just a keyword used to "spell" foreach. Commented Dec 20, 2016 at 15:55

1 Answer 1

1

There are several problems here:

  • Invalid while loop syntax
  • Unnecessary loop: it seems you don't need a loop at all
  • Missing closing fi of an if that was opened

I suppose you're looking for something like this:

#!/bin/bash
status=$(/etc/init.d/rsyslog status | grep -o 'running\|dead')
case "$status" in
dead) echo "service rsyslog is dead";;
running) echo "service rsyslog is running";;
*) echo "nothing to do";;
esac
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.