我找到的最简单的解决方案是:
[^0]+
这方面的主要问题是它与以"0"开头的数字不匹配,例如"0123"。除此之外,它非常通用,可以匹配所有非"0"的内容,包括字母和特殊字符。
编辑:
以下是整个脚本,也许这有助于找到解决方案:
search=$(find / -iname _instanceBackup.log 2>/dev/null)
mytail=$(tail -n 14 $search)
mydate=$(date -dyesterday +%y%m%d)
if [[ $mytail =~ $mydate ]]
then
echo "Backup is up to date!"
mytail=${mytail,,}
if [[ ( $mytail =~ 'failed' ) || ( $mytail =~ 'backupexitcode:'[missing RegEx] ) ]]
then
echo "An error has occurred in the backup!"
exit 1001
else
echo "No errors were found in the last backup."
exit 0
fi
else
echo "Backup hasn't run!"
exit 1002
fi
第二个if语句是我试图用RegEx做的。不幸的是,我不知道哪些备份退出代码是可能的,但从最近的日志中,我可以说它们是两位数,可以是负数,可以以0开头。
$mytail
看起来像这样:
----------------------------
run_backup.sh(24778) 220222-200037: Instance:user@server:port Schema:"backupname" [Additional Information]
run_backup.sh(24778) 220222-200037: Instance:user@server:port Schema:"backupname" [Additional Information]
run_backup.sh(24778) 220222-200037: Instance:user@server:port Schema:"backupname" [Additional Information]
run_backup.sh(24778) 220222-200037: Instance:user@server:port Schema:"backupname" [Additional Information]
INFO SAP BACKUP UTILITY
INFO Creating backup of server [server:port] in folder [Backupfolder].
INFO Server backup operations finished without error.
run_backup.sh(24778) 220222-200056: Instance:user@server:port Schema:"backupname" [Additional Information]
INFO SAP BACKUP UTILITY
INFO Older backups were deleted without error.
run_backup.sh(24778) 220222-200140: Instance:user@server:port Schema:"backupname" Finish: Timestamp:220222_200037 BackupExitCode:58
----------------------------
注意,要匹配不全为零的数字字符串,可以使用
^-?0*[1-9][0-9]*$
这里,^
匹配字符串起始位置,-?
匹配可选的-
,然后0*
匹配零个或多个0
数字,[1-9]
模式匹配非零数字,并且然后[0-9]*
匹配零个或者多个数字,并且$
断言字符串末尾的位置。
你可以使用
rx='backupexitcode:-?0*[1-9][0-9]*([^0-9]|$)'
if [[ "$mytail" == *failed* || "$mytail" =~ $rx ]]; then
# the rest of the script
Regex详细信息:
backupexitcode:
-文字backupexitcode:
字符串-?
-可选的-
字符0*
-零个或多个0
字符[1-9]
-非零数字[0-9]*
-任意零位或多个数字([^0-9]|$)
-非数字字符或字符串结尾
"$mytail" == *failed*
部分使用的是glob,而不是regex模式,*
通配符匹配任何零个或多个字符,glob模式必须匹配整个字符串(因此,两端的*
都是必需的,因为failed
应该在mytail
字符串内的任何位置检测到(。