9

I want to write a script to check out if the website is working fine by checking out some of it is contents. If the content exists in the result, it prints a message telling the website is working fine, otherwise, it will show an error:

#!/bin/bash

webserv="10.1.1.1" 

Keyword="helloworld" # enter the keyword for test content


if (curl -s "$webserv" | grep "$keyword") 
        # if the keyword is in the conent
        echo " the website is working fine"
else
        echo "Error"

Any suggestion how to do that?

2 Answers 2

16

You're mostly there. Just fix your syntax:

if curl -s "$webserv" | grep "$keyword"
then
    # if the keyword is in the conent
    echo " the website is working fine"
else
    echo "Error"
fi

Note the then and fi.

1
  • 1
    Use grep -q instead.
    – Amit Naidu
    Jul 29, 2019 at 3:25
1

A tiny amend: when setting the variable and using it later, the case needs to match in the two places (ie 'keyword', not 'Keyword'). The full code that works for me:-

#!/bin/bash

webserv="10.1.1.1" 

keyword="helloworld" # enter the keyword for test content

if curl -s "$webserv" | grep "$keyword"
then
    # if the keyword is in the content
    echo " the website is working fine"
else
    echo "Error"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.