我想用我的脚本来完成:
但实际上不管用,我也不知道为什么。
它会发邮件给我WAN2.txt文件,不管它是否相同,它也不会覆盖它。
#!/bin/bash
wan1=/home/user/Scripts/wanip.txt
wan2=/home/user/Scripts/wanip2.txt
dig +short myip.opendns.com @resolver1.opendns.com>$wan2
if [ "$wan1" != "$wan2" ]
then
/usr/bin/sendEmail -f showname@example.com -t sendto@example.com -u Wanip -m "hallo" -s smtp.example.com:587 -xu user -xp passwort -a $wan2
elif [ "$wan1" != "$wan2" ]
then
$wan2>$wan1
else exit
fi 发布于 2017-11-30 18:31:59
您正在比较变量,而不是文件的内容。
在您的示例中,变量$wan1将永远不会保存与$wan2相同的值,因此脚本将始终向您发送电子邮件。使用cmp、diff等比较文件。
而且,您的if和elif也有相同的条件,这意味着当if被触发时,elif不会触发。
$wan1 > $wan2也不正确,因为它会搜索名为/home/user/Scripts/wanip.txt的程序并将输出重定向到/home/user/Scripts/wanip2.txt。使用cp覆盖或更改行为cat "$wan2" > "$wan1"。
#!/bin/bash
wan1=/home/user/Scripts/wanip.txt
wan2=/home/user/Scripts/wanip2.txt
dig +short myip.opendns.com @resolver1.opendns.com > "$wan2"
if ! cmp --quiet "$wan1" "$wan2"; then
/usr/bin/sendEmail -f showname@example.com -t sendto@example.com -u Wanip -m "hallo" -s smtp.example.com:587 -xu user -xp passwort -a "$wan2"
cp -f "$wan2" "$wan1"
fi https://stackoverflow.com/questions/47578195
复制相似问题