bash script to check website content by curl command

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?


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.


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