bash变量,如果语句没有正确评估

时间:2017-07-31 14:54:55

标签: bash if-statement

我写了一个脚本,应该告诉我夜间网络备份的行数。它应该是109,如果它是相同的,我会收到成功的电子邮件,如果它不一样,我会收到一封失败的电子邮件。我已将2个假主机添加到下面脚本中检查的其中一个文件中,以查看它是否会失败。 if then语句根本不起作用。如果我在文件中确认主机数量并确认备份'并且'备份'不同它没有区别,它只是使用第一个if then语句,无论它们是否不同。

最后,您可以看到我在两个文件上运行了wc -l,但它们不同,但是脚本运行并给我第一个if,然后:备份和确认备份匹配。它也是另一种方式 - 如果它们是相同的我只是得到第一个if then语句 - 这首先让我觉得它工作,直到我检查它与数字不匹配的文件。

#!/bin/bash

# Variables
date=`date +%Y%m%d`
o1=$(cat /netops/backups/scripts/hostfiles/backed-up | wc -l)
o2=$(cat /netops/backups/scripts/hostfiles/confirm-backed-up | wc -l)
sdir=/netops/backups/storage/
hostdir=/netops/backups/scripts/hostfiles

#Functions

function confirm_backup
{
find $sdir -type f -mtime 0 -printf '%f\n' |grep $date >$hostdir/backed-up
cat $hostdir/cisco-nexus.txt >> $hostdir/confirm-backed-up
cat $hostdir/cisco-firewall.txt >> $hostdir/confirm-backed-up
cat $hostdir/esx.txt >> $hostdir/confirm-backed-up
cat $hostdir/f5.txt >> $hostdir/confirm-backed-up
cat $hostdir/fortigate.txt >> $hostdir/confirm-backed-up
cat $hostdir/rsa.txt >> $hostdir/confirm-backed-up
cat $hostdir/sw-no-pk.txt >> $hostdir/confirm-backed-up
cat $hostdir/switch-router.txt >> $hostdir/confirm-backed-up
cat $hostdir/tlite.txt >> $hostdir/confirm-backed-up
}

# Verify Backup

function backup_verify
{
if [ "echo $o1" == "echo $o2" ]; then # I tried this with if [ "$o1" == "$o2"] also & if (( $o1 != $o2 )) & [ "$o1" = "o2" ] - all same results.
echo "backed-up and confirm-backed-up match" & mail -s "All Backups   succeeded" 12345566@blah.net   < /dev/null
else
echo "a backup has failed" & mail -s "A backup failed" 123456789@vtext.com  < /dev/null
fi
}

# Start Script Run

confirm_backup
backup_verify
cat /dev/null > $hostdir/confirm-backed-up # this is here for long term - i tested it with this gone so otherwise obviously my wc -l would have been 0
cat /dev/null > $hostdir/backed-up

user@host:/netops/backups/scripts$ ./test6.sh 
backed-up and confirm-backed-up match
Null message body; hope that's ok # WRONG!
user@host:/netops/backups/scripts$ cd hostfiles/
user@host:/netops/backups/scripts/hostfiles$ wc -l backed-up 
109 backed-up # so I check manually
user@host:/netops/backups/scripts/hostfiles$ wc -l confirm-backed-up 
111 confirm-backed-up # the files are different.

1 个答案:

答案 0 :(得分:1)

我相信您应该设置变量 后填充文件backed-upconfirm-backed-up。否则,文件中的行数为空,if条件为真(0 = 0)。

所以尝试改变这个:

# Start Script Run

confirm_backup
backup_verify
cat /dev/null > $hostdir/confirm-backed-up # this is here for long term - i tested it with this gone so otherwise obviously my wc -l would have been 0
cat /dev/null > $hostdir/backed-up

对此:

# Start Script Run

confirm_backup
o1=$(wc -l < /netops/backups/scripts/hostfiles/backed-up)
o2=$(wc -l < /netops/backups/scripts/hostfiles/confirm-backed-up)
backup_verify
cat /dev/null > "$hostdir"/confirm-backed-up # this is here for long term - i tested it with this gone so otherwise obviously my wc -l would have been 0
cat /dev/null > "$hostdir"/backed-up

此外,对于数字比较,请使用-eq并删除echo

if [ "$o1" -eq "$o2" ]; then
    # do stuff
fi

希望有所帮助。

相关问题