这是我的run.bash
文件(如果我删除$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
字符串
这是file.txt
:
G9DG1.fqdn.com
G9DG2.fqdn.com
G9DG3.fqdn.com
G9DG4.fqdn.com
G9DC3.fqdn.com
G9DG5.fqdn.com
G9DC4.fqdn.com
型
这是/home/config.conf
和/home/config.conf-bk
,它们在每台服务器上都应该相同:
# this is a test config file
型
这是sha256sum config.conf
:
91c17dcc7bc4c48594c0d8eb9726b97e5e2c6aaf728779c9e39a69fe76229c9e config.conf
型
此时,sha256sum
这个文件的值与config.conf-bk
文件相同。现在,如果我运行脚本,它会打印:
The config.conf file in G9DG1.fqdn.com has changed, so we reverted the file
型
我也把我的剧本改成:
#!/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
型
但这一次我明白了:
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.
型
预期的输出是检查所有服务器(不仅在第一次匹配if和terminate子句之后),检查文件是否更改,然后执行if-else
子句。
更新1
下面的bash文件现在在整个文件中循环,但始终回显两个内部循环的else
部分:
#!/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
型
当前(意外)输出:
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.
型
2条答案
按热度按时间vql8enpb1#
你的逃跑计划取消了。您有一个用双引号括起来的awk命令:
字符串
awk
脚本使用单引号,但这些引号是伪引号;它们不是引号语法,因为它们在双引号中!$1
根本不受单引号保护;它暴露于双引号,其中像$1
这样的扩展是完全有效的。如果你的脚本没有被赋予任何参数,那么
$1
将扩展为空,而你的awk
代码只是{ print }
,它将包括带有SHA 256的路径名。路径名不相等,因此…尝试使用
awk '{ print \$1 }'
来消除美元符号的特殊含义,并保留$1
,以便它可以通过ssh
连接的另一端到达shell。qlckcl4x2#
看看这个
字符串