2

So I have a pattern like somepattern-abc-1.2.3-aji3mk3-11.tar. I need to add a condition where

if [ $input matches somepattern[].tar ] then do something fi 

here the somepattern will be a constant one, rest all till ".tar" will vary and can have a hyphen or a letter or a number. What can I replace my [] with so that it will be satisfied?

7
  • 1
    Can you please try to reword the following sentence, I am unable to make any sense of it: rest all till ".tar" will vary and can have a hyphen or a letter or a number. Commented Nov 14, 2019 at 16:40
  • Actually I think I may understand it but you will need to tell us exactly what somepattern is. Commented Nov 14, 2019 at 16:41
  • somepattern is something like "operator" Commented Nov 14, 2019 at 16:43
  • so in between operator and .tar we can have any pattern and this pattern will contain alphabet or number or - Commented Nov 14, 2019 at 16:44
  • And also a dot .? Commented Nov 14, 2019 at 16:44

2 Answers 2

3

You can test with this:

input="somepattern-abc-1.2.3-aji3mk3-11.tar" [[ "$input" =~ ^somepattern[-.a-z0-9]*\.tar$ ]] && echo "match" # here your command 
0
1

If you can write the pattern as a regexp then you have the bash operator =~ that will compare the first argument with the regular expression on the other side.

You can test it with something like:

read a;[[ "$a" =~ "x" ]] && echo $a 

where "x" is your pattern.

1
  • Note that if you quote the right hand side of =~, it becomes literal. ".*", for example, would match a dot followed by an asterisk. Commented Nov 14, 2019 at 17:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.