0

This code check if the last 4 characters of the variable $1 correspond to the string in variable $2 anticipated by a dot.

    if [ "${1: -4}" == ".$2" ]; then
        return 0
    else
        return 1
    fi

// true with $1 = example.doc and $2 = doc
// false with $1 = example.docx and $2 = doc

how can I replace the hardcoded 4 with the following variable $CHECKER_EXTENSION calculated like this?

    LENGTH_EXTENSION=${#2}
    CHECKER_EXTENSION=$(( LENGTH_EXTENSION + 1 ))

Thanks in advance

6
  • Please add your desired output (no description) for that sample input to your question (no comment). Commented Apr 29, 2020 at 19:44
  • 1
    If the part you wish to get is always behind a dot (.) you may use: ${url##*.} instead of counting the number of chars you need to remove Commented Apr 29, 2020 at 19:46
  • @0stone0: That's a good point; but note that it will give the wrong result if $2 contains a dot (if that's possible). Commented Apr 29, 2020 at 19:52
  • @Cyrus I needed to know how to replace the hardcoded 4 with a variable, the goal is to check if a filename and a given extension are the same, but that was not the question. I use the dot as an extra check in the filename. Commented Apr 29, 2020 at 20:07
  • Replace 4 with $variable. Commented Apr 29, 2020 at 21:43

4 Answers 4

3

You don't need to strip the leading characters from $1, since bash's [[ ]] can do wildard-style pattern matching:

if [[ "$1" = *".$2" ]]; then
    ...

Note that you must use [[ ]], and not [ ], to get pattern-matching rather than simple string equality testing. Also, having the * unquoted but .$2 in quotes means the * will be treated as a wildcard, but $2 will be matched literally even if it contains wildcardish characters. If you want $2 to also be treated as a pattern (e.g. you could use [Jj][Pp][Gg] to match "jpg" and "JPG" and combinations), leave off the quotes:

if [[ "$1" = *.$2 ]]; then

Oh, and the quotes around $1 don't matter in this particular situation; but I tend to double-quote variables unless there's a specific reason not to.

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

1 Comment

+1. This is one of those cases where what the OP is asking for isn't really what the OP should use in his/her specific situation. :-)
2

The offset is interpreted as an arithmetic expression (the same syntax as inside $(( ... ))), so you can write:

    if [ "${1: -CHECKER_EXTENSION}" == ".$2" ]; then

You can even eliminate the CHECKER_EXTENSION variable and write:

    if [ "${1: -(${#2} + 1)}" == ".$2" ]; then

Comments

1

You may use it like this:

myfunc() {
   local num_ext=$(( ${#2} + 1 ))

   [[ "${1: -$num_ext}" = ".$2" ]]
}

Comments

0

If your execution environment meets the following criteria, you can choose a more concise method.

  • The requirement is to check if the extension is as expected
  • I'm using Bash
#!/usr/bin/env bash

if [[ ${1##*.} == "${2}" ]]; then
  echo "same"
else
  echo "not same"
fi

Execute test.

# test case 1
$ ./sample.sh test.txt txt
same

# test case 2
$ ./sample.sh test.exe txt
not same

# test case 3
$ ./sample.sh test.exe.txt txt
same

# test case 4
$ ./sample.sh test.txt.exe txt
not same

# test case 5
$ ./sample.sh test.txt.exe exe
same

Comments

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.