0

I have the following shell script in place to send out an email alert. When I only have the for loop everything works fine but when I try to add the if condition it does not seem to work and returns a null message body everytime. A snippet from the script is as below:

for host in $servers
do
connections=`ssh $host "netstat -a | grep ES | wc -l"`
echo "$connections" >> debug.log
echo "$host" >> debug.log

if [$connections -gt 0]
then
echo "------------------------------------------------------------------" >> $emailmessage
echo "Host: $host needs to be checked" >> $emailmessage
echo "------------------------------------------------------------------" >> $emailmessage
echo "$connections" >> $emailmessage
else
echo "Everything is fine"
fi

done

I always end up getting the line 21: [49: command not found error when i try to execute the script. I tried debugging but I figured out my script is not even getting inside the if loop.

Can someone please let me know what am I missing?

Thanks in advance.

/rd

1
  • Terminology quibble: an if statement is not a loop. Commented Nov 11, 2013 at 20:29

1 Answer 1

1

There should be space around [ and ] also its better to put variable in quotes, so update your line as

...
if [ "$connections" -gt 0 ] ; 
then
...
Sign up to request clarification or add additional context in comments.

1 Comment

@rond As a tip for the future, Shellcheck will automatically point out silly mistakes like this.

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.