1

The easiest solution I found would be:

[^0]+

The main problem with this is that it doesn't match numbers that begin with a '0' e.g. '0123'. In addition to that it's very general and would match everything that isn't '0' including letters and special characters.

Edit:

Here's the whole script, maybe this helps finding a solution:

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

The 2nd if-statement is what I'm trying to do with RegEx. Unfortunately I don't know exactly which backupexitcodes are possible but from recent logs I can say that they are 2-digit numbers that can be negative and can begin with a 0.

$mytail looks like this:

----------------------------
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
----------------------------
Icoryx
  • 96
  • 7
  • 2
    Maybe all you need is to match a digit only string with at least one non-zero digit? `^0*[1-9][0-9]*$`? Or, with `-`: ``^-?0*[1-9][0-9]*$``? – Wiktor Stribiżew Feb 23 '22 at 11:47
  • 2
    A little more info about the use-case may help. For example - you said it can be smaller than 0 - so as @WiktorStribiżew said you may need the -? at the start. Separately, is "00" or "000" acceptable"? Based on your specific use-case, there may be a nicer way to frame the problem - but very likely I think Wiktor's suggestion will help you. – Chris Feb 23 '22 at 12:17
  • Which if any of the following are possible inputs: `-0`, `-00`, `00`, `-000`, `000`? – Bohemian Feb 23 '22 at 14:00
  • @chrisputnam9 I updated my question, maybe it's easier to understand now. "00" or "000" shouldn't be matched. – Icoryx Feb 23 '22 at 14:02
  • @WiktorStribiżew 's soltution seems to be working atlest when testing with [link](regexr.com) – Icoryx Feb 23 '22 at 14:03
  • Ok, what about the code? How are you planning to use it? `rx='^-?0*[1-9][0-9]*$'; if [[ "$text" =~ $rx ]]; then ... fi`? – Wiktor Stribiżew Feb 23 '22 at 14:26
  • @WiktorStribiżew yeah something like that. I have tried implementing the regex directly into the if-statement as follows `if [[ ( $mytail =~ 'failed' ) || ( $mytail =~ "backupexitcode:"^-?0*[1-9][0-9]*$ ]]` but that isn't working. I'm not really sure if I have to put it in brackets or something? – Icoryx Feb 23 '22 at 14:53
  • Try `rx='backupexitcode:-?0*[1-9][0-9]*([^0-9]|$)'; if [[ "$mytail" == *failed* || "$mytail" =~ $rx ]]`.... What does the `mytail` look like? – Wiktor Stribiżew Feb 23 '22 at 15:14
  • @WiktorStribiżew works perfectly. I added a reconstruction of `$mytail` to the question. Should I add this as an answer and mention you or do you prefer to add it yourself so I could mark it as an accepted answer? – Icoryx Feb 23 '22 at 16:14
  • I hope my answer is complete enough. Let me know if I missed anything. – Wiktor Stribiżew Feb 23 '22 at 16:58

1 Answers1

1

Note that to match a numeric string that is not all zeros you can use

^-?0*[1-9][0-9]*$

Here, ^ matches the string start position, -? matches an optional -, then 0* matches zero or more 0 digits, a [1-9] pattern matches a non-zero digit and then [0-9]* matches zero or more digits and $ asserts the position at the end of string.

You can use

rx='backupexitcode:-?0*[1-9][0-9]*([^0-9]|$)'
if [[ "$mytail" == *failed* || "$mytail" =~  $rx ]]; then
# the rest of the script

Regex details:

  • backupexitcode: - a literal backupexitcode: string
  • -? - an optional - char
  • 0* - zero or more 0 chars
  • [1-9] - a non-zero digit
  • [0-9]* - any zero or more digits
  • ([^0-9]|$) - a non-digit char or end of string.

The "$mytail" == *failed* part is using a glob, not a regex pattern, * wildcard matches any zero or more chars and the glob pattern must match the whole string (hence, * on both ends are required as failed should be detected anywhere inside the mytail string).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563