0

I'm a newbie in the linux environment, and I'm starting to create an automated smoke test for several commands we frequently use at our company. Basically, running some kind of shell script that runs through multiple commands and also validates the command's output.

The first test cases I started writing out was to check our service can be successfully stopped and started. After researching around about bash scripts I came up with this:

#!/bin/bash

sudo service companyservice stop | grep 'Stopping companyservice ... [  OK  ]' &> /dev/null \
if [ $? == 0 ] then echo "Stopping Company Service: SUCCESS" \
else    echo "Stopping Company Service: FAIL. GO HARASS A DEVELOPER" \
fi

sudo service companyservice start | grep 'Starting companyservice ... [  OK  ]' &> /dev/null \
if [ $? == 0 ] then echo "Starting Company Service: SUCCESS" \
else    echo "Starting Company Service: FAIL. GO HARASS A DEVELOPER" \
fi

I saved this as SmokeTest.sh, but when running sh SmokeTest.sh on command line, I see nothing on the output. No error, no failure, no success. Nothing.

Any help or hints with this is much appreciated. I am using Red Hat 6.6 OS. Also should this be right way to automate on Linux if I want to validate command's outputs?

2 Answers 2

1

Your line continuation characters \ at the end of the sudo lines are making the if part of the command line you're running. Get rid of those, and you should start to see syntax errors because you don't have ; after the conditions for your if statements before then

Also, on the lines with the continuation characters you're redirecting stderr to /dev/null which is why you wouldn't see it complaining about the situation

As you noted, it's possible to not put the ; with an if, but if you do so the then must be on the next line:

if [ -z "$var" ] then

Is wrong but

if [ -z "$var" ]; then

or

if [ -z "$var" ]
then

are both acceptable.

Also, the single line continuation characters might have been a little lost. If a line of bash ends with \ it means that the following line should actually be treated as part of the current line. So in your example:

sudo service companyservice stop | grep 'Stopping companyservice ... [  OK  ]' &> /dev/null \
if [ $? == 0 ] then echo "Stopping Company Service: SUCCESS" \
else    echo "Stopping Company Service: FAIL. GO HARASS A DEVELOPER" \
fi

is actually treated as a single line like

sudo service companyservice stop | grep 'Stopping companyservice ... [  OK  ]' &> /dev/null if [ $? == 0 ] then echo "Stopping Company Service: SUCCESS" else    echo "Stopping Company Service: FAIL. GO HARASS A DEVELOPER" fi

which is not right. If you remove the \ from each line and settle on a way to do the if...then you should be in much better shape

Sign up to request clarification or add additional context in comments.

7 Comments

From what I read here thegeekstuff.com/2010/06/bash-if-statement-examples if statements on bash scripts don't need ; .. but following your advice and also removing /dev/null now returns: SmokeTest.sh: line 12: syntax error: unexpected end of file
Alos I read somewhere that bash script are supposed to contain one command per line which is why I used line continuation characters.
There are a couple of things at play, too much to put in a comment, so I'm going to amend my answer to try to make it more clear
@kevinb I forgot to tag you in the last comment, but I updated my answer to try to make things a little more clear for you
Thank you @eric-renouf. Followed it and now I'm actually getting output. Only thing that now validation is showing a failure when it's supposed to show success. Perhaps I'm not using grep to validate the string correctly.. but that's outside of the scope for this question. Thanks!
|
1

&> /dev/null will direct any output/errors to /dev/null and you will see no output or error. remove these parts or redirect them to a file for exmple:

&> log.txt

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.