0

I have the following check:

if [[ "$abc" =~ ^((git|ssh|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([A-Za-z0-9.@:_/-]+)\.com(:[0-9]+)?\/([A-Za-z0-9.@:_/-]+)\/([a-zA-Z0-9_]+)(\.git)(\/)?$ ]]; then
   die "Invalid"
fi

It does not really matter what it checks, I just want to get rid of the =~ operator. I have a basic static test of the scripts:

for file in $filelist
do
  echo -n "Checking $file..."
  bash -n $file
  echo "OK"
done 

Due to the =~ operator, it gets an error:

syntax error in conditional expression: unexpected token `('
script: line 847: syntax error near `^(('

Even though, when I execute the script itself, I don't get that error. I guess the error is due to not wrapping ^((git|ssh|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([A-Za-z0-9.@:_/-]+)\.com(:[0-9]+)?\/([A-Za-z0-9.@:_/-]+)\/([a-zA-Z0-9_]+)(\.git)(\/)?$ with apostrophes. If I do wrap it, it will not enter the if statement. So I'm looking to replace the =~. What is the proper way to do so?

11
  • I'm unable to reproduce this. I put your check in a file foo and ran bash -n foo and it completed without errors. Commented Dec 20, 2019 at 23:53
  • @thatotherguy Sorry, I thought it was a problem with =~ and not a problem with the regex itself. I updated the full regex. It a problem with the regex? Commented Dec 20, 2019 at 23:55
  • I'm unable to reproduce the problem with this new regex as well. Commented Dec 20, 2019 at 23:58
  • @thatotherguy I'm not sure why but I get this behavior. Is it possible to use that regex with the apostrophes? Commented Dec 20, 2019 at 23:59
  • Can you please try it with just your 3 line check and your 6 line test loop? I.e. keep your existing 850+ line scripts out of it and just use what you've posted to ensure that the post contains all relevant information Commented Dec 21, 2019 at 0:00

1 Answer 1

1

Consider using egrep -q instead:

if [[ "$abc" =~ ^((git|ssh|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([A-Za-z0-9.@:_/-]+)\.com(:[0-9]+)?\/([A-Za-z0-9.@:_/-]+)\/([a-zA-Z0-9_]+)(\.git)(\/)?$ ]]; then
   die "Invalid"
fi

becomes:

if  
    egrep -q "^((git|ssh|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([A-Za-z0-9.@:_/-]+)\.com(:[0-9]+)?\/([A-Za-z0-9.@:_/-]+)\/([a-zA-Z0-9_]+)(\.git)(\/)?$ ]]" <<< "$abc"
then
   die "Invalid"
fi

egrep -q will return true (exit code of 0) if the regexp matches stdin (in this case we are using $abc as the stdin input.

I used egrep because you have an or expression in there (the pipes).

To be honest, I haven't tested this code, but I've used the -q option before with grep. You'll need to make sure that your regexp is correct.

Sign up to request clarification or add additional context in comments.

1 Comment

Only hit with egrep (or any utility) would depend on how many times the if is called. If it is called in a loop that iterates 1M times, then that would add 1M subshells spawned dramatically slowing the script. If it's a one-off, then no issue at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.