This is my run.bash
file (in which I have problem that I can't iterate over the file completely if I remove $line = ...
):
#!/usr/bin/bash
while IFS= read -r line
do
if [ "$(ssh -p2200 "$line" "sha256sum /home/config.conf | awk '{print $1}'")" == "$(ssh -p2200 "$line" "sha256sum /home/config.conf-bk | awk '{print $1}'")" ]
then
echo "File is the same in \"$line\"." > /dev/null 2>&1 # this output is not important
else
echo "The config.conf file in $line has changed, so we reverted the file." | mail -s "The config.conf file in $line has changed" myemail@gmail.com
ssh -np2200 "$line" cp -a /home/config.conf-bk /home/config.conf
fi
done < file.txt
This is file.txt
:
G9DG1.fqdn.com
G9DG2.fqdn.com
G9DG3.fqdn.com
G9DG4.fqdn.com
G9DC3.fqdn.com
G9DG5.fqdn.com
G9DC4.fqdn.com
This is the /home/config.conf
and /home/config.conf-bk
which should be the same on each server:
# this is a test config file
This is sha256sum config.conf
:
91c17dcc7bc4c48594c0d8eb9726b97e5e2c6aaf728779c9e39a69fe76229c9e config.conf
At the moment, the value of sha256sum
this file is the same as config.conf-bk
file. Now if I run my scripts, it prints:
The config.conf file in G9DG1.fqdn.com has changed, so we reverted the file
I also changed my script to:
#!/usr/bin/bash
while IFS= read -r line
do
if [[ "$line" =~ ^([Gg][0-9]{1,2}[DS][C][0-9]{1,3}.fqdn.com)$ ]] # C-Plans
then
if [ "$(ssh -p2200 "$line" "sha256sum /home/config.conf | awk '{print $1}'")" == "$(ssh -p2200 "$line" "sha256sum /home/config.conf-bk | awk '{print $1}'")" ]
then
echo "File is the same in \"$line\"." > /dev/null 2>&1 # this output is not important
else
echo "The config.conf file in \"$line\" has changed, so we reverted the file." | mail -s "The config.conf file in \"$line\" has changed" myemail@gmail.com
ssh -np2200 "$line" cp -a /home/config.conf-bk /home/config.conf
fi
elif [[ "$line" =~ ^([Gg][0-9]{1,2}[DS][G][0-9]{1,3}.fqdn.com)$ ]] # G-Plans
then
if [ "$(ssh -p2200 \"$line\" "sha256sum /home/config.conf | awk '{print $1}'")" == "$(ssh -p2200 \"$line\" "sha256sum /home/config.conf-bk | awk '{print $1}'")" ]
then
echo "File is the same in $line." > /dev/null 2>&1 # this output is not important
else
echo "The config.conf file in $line has changed, so we reverted the file." | mail -s "The config.conf file in $line has changed" myemail@gmail.com
ssh -np2200 "$line" cp -a /home/config.conf-bk /home/config.conf
fi
fi
done < file.txt
But this time I see this:
File is the same in G9DG1.fqdn.com.
File is the same in G9DG2.fqdn.com.
File is the same in G9DG3.fqdn.com.
File is the same in G9DG4.fqdn.com.
The config.conf file in G9DC3.fqdn.com has changed, so we reverted the file.
The expected output is to check all servers (not only after the first matching if and terminate the clause) and check if file is changed or not, and then do the if-else
clause.
Update 1
The below bash file now loops all over the file, but always echos the else
part of both two inner loops:
#!/usr/bin/bash
while IFS= read -r line
do
if [[ "$line" =~ ^([Gg][0-9]{1,2}[DS][C][0-9]{1,3}.fqdn.com)$ ]] # C-Plans
then
if [ "$(ssh -np2200 $line "sha256sum /home/config.conf | awk '{print $1}'")" == "$(ssh -np2200 $line "sha256sum /home/config.conf-bk | awk '{print $1}'")" ]
then
echo "It is the same in $line."
else
echo "The config.conf file in $line has changed, so we reverted the file."
fi
elif [[ "$line" =~ ^([Gg][0-9]{1,2}[DS][G][0-9]{1,3}.fqdn.com)$ ]] # G-Plans
then
if [ "$(ssh -np2200 $line "sha256sum /home/config.conf | awk '{print $1}'")" == "$(ssh -np2200 $line "sha256sum /home/config.conf-bk | awk '{print $1}'")" ]
then
echo "It is the same in $line."
else
echo "The config.conf file in $line has changed, so we reverted the file."
fi
fi
done < file.txt
The current (unexpected) output:
It is not the same in G1DG1.fqdn.com.
It is not the same in G1DG2.fqdn.com.
It is not the same in G1DG3.fqdn.com.
It is not the same in G1DG4.fqdn.com.
It is not the same in G1DC3.fqdn.com.
It is not the same in G1DG5.fqdn.com.
It is not the same in G1DC4.fqdn.com.