0

I have a variable called PARAM_COMPARE which is set to *.md.

I'd like to compare filenames from GitHub Commits endpoint against PARAM_COMPARE.

gh api repos/"$PARAM_GH_PROJECT"/"$PARAM_GH_REPO"/commits/"$PARAM_GH_SHA" | jq -r '.files[].filename' | while read filename; do
  echo "${filename}" "${PARAM_COMPARE}"
  if [[ $filename =~ $PARAM_COMPARE ]]; then
    echo matched
    break
  fi
done

This sadly says it is not a match, even when the filename is README.md.

Have I forgotten something in this script?

4
  • 1
    You're mixing up shell patterns and regular expressions. Is this a good duplicate? How to check if a file name matches regex in shell script Commented Sep 11, 2023 at 15:43
  • 3
    *.md is not a regex, it is a glob pattern. Use [[ $filename = $PARAM_COMPARE ]] Commented Sep 11, 2023 at 15:43
  • 1
    @anubhava Or better: use case $filename in $PARAM_COMPARE ) ... ;; esac Commented Sep 11, 2023 at 16:14
  • Thank you @anubhava @Benjamin W. @F. Hauri. I opted to change it to a blob pattern but also include another if block for regex matching. ` if [[ $filename =~ $PARAM_COMPARE ]]; then echo "matched regex" break fi # shellcheck disable=SC2053 if [[ $filename == $PARAM_COMPARE ]]; then echo "matched glob" break fi ` It's not the most elegant but I want to be able to cover whatever a user may pass to PARAM_COMPARE. Commented Sep 12, 2023 at 11:53

0

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.