[[+content_image]]
J
J
jenya77712021-01-09 16:50:54
bash
jenya7771, 2021-01-09 16:50:54

How to make a regular expression in bash script?

Hello, please explain how can I make it so that

day = 1
item = "backups_all_ 2021.01.08|15.25.30.tar.gz"
d="$(date +%Y-%m-%d -d "$day days ago")"

if [[ item =~ "backups_all_$d|*.tar.gz" ]]
then
            echo "ok"
fi


How can I integrate a regular expression so that after the slash (|) there can be any time and the response is OK?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
Saboteur, 2021-01-10
@jenya7771

Remove spaces around =
check that you use the correct characters everywhere, otherwise you write 2021.01.08 in item with a dot, and in date you write %Y-%m-%d with a hyphen.
In the regular expression, remove the quotes and replace the asterisk with a dot with an asterisk.

#!/bin/bash

day=2
item="backups_all_2021-01-08|15.25.30.tar.gz"
d="$(date +%Y-%m-%d -d "$day days ago")"

if [[ $item =~ backups_all_$d|.*.tar.gz ]]; then
  echo "ok"
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question