Answer the question
In order to leave comments, you need to log in
[[+content_image]]
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question