0

I have 4 domains that I want to check in my cron every hour. It checks if a word exists, and if it doesn't it will reboot the machine. In my example below I have 4 domains I am checking, but how do I loop through these variables in an if statement without having to replicate this 4 times in my bash script.

#!/bin/bash

webserv1="domain1.com"
webserv2="domain2.com"
webserv3="domain3.com"
webserv4="domain4.com"

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


if (curl -s "$webserv1" | grep "$keyword") 
then
        echo " the website is working fine"
else
        sudo reboot
fi

if (curl -s "$webserv2" | grep "$keyword") 
then
        echo " the website is working fine"
else
        sudo reboot
fi

if (curl -s "$webserv3" | grep "$keyword") 
then
        echo " the website is working fine"
else
        sudo reboot
fi

if (curl -s "$webserv4" | grep "$keyword") 
then
        echo " the website is working fine"
else
        sudo reboot
fi
2
  • 1
    Instead of separate variables put them all into an array Commented Sep 18, 2019 at 19:07
  • The parentheses around conditions aren't necessary. Commented Sep 18, 2019 at 21:12

4 Answers 4

3

Array approach would be :

arr=(a1.com a2.com a3.com) ## Define an array with values

#Loop through all the array values

for val in "${arr[@]}"
do
echo $val
done

The output would be :

a1.com
a2.com
a3.com

Your script would look like this :

webservers=(domain1.com domain2.com domain3.com domain4.com)
Keyword="helloworld"

for webserver in "${webservers[@]}"
do
    if (curl -s "$webserver" | grep "$keyword")
    then
        echo " the website $webserver is working fine"
    else
        sudo reboot
    fi
done
Sign up to request clarification or add additional context in comments.

4 Comments

"${arr[@]}", please. It's a good habit, and it costs almost nothing.
@rici Done. Thanks.
The array is a convenience; the same loop could be written by hardcoding the sequence instead of defining the array first.
@chepner That is correct. But if in case we want to use it in multiple loops then I think it would become tedious to write the hardcoding sequence again and again. That is why I just defined it on top.
1

You would make an array and loop through like this:

for d in domain1.com domain2.com domain3.com
do
  echo $d
done

Comments

0

Updated using chepner answer and also added in another parameter:

#!/bin/bash

sv2="http://yyyy/get_info"
sv3="http://zzzz/get_info"
key1="xxxx"
key2="yyy"
key3="uuu"

check () {
if curl -s "$1" | grep "OK"; then
echo "${1} ${2} - GOOD - $(date)"
else
curl -H 'API-Key: XXXXX' https://____/server/reboot --data "ID=${2}"
echo "REBOOT ----- ${1}"
fi
}
check "$sv1" "$key1"
check "$sv2" "$key2"
check "$sv3" "$key3"sv1="http://xxx/get_info"

Comments

-1

Note that you can reduce repetition without necessarily using a loop. Define a function to abstract out the common part, then call the function multiple times.

#!/bin/bash

webserv1="domain1.com"
webserv2="domain2.com"
webserv3="domain3.com"
webserv4="domain4.com"

check () {
  if curl -s "$1" | grep "$keyword"; then
    echo "the website $1 is working fine"
  else
    sudo reboot
  fi
}

check "$webserv1"
check "$webserv2"
check "$webserv3"
check "$webserv4"

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.