0

I want to call a function ONLY when the file name ends with .request but somehow it calls the function if request is sub-string.

I marked with red and green the imported parts, on the right it's the output

for file in ${filesOrDir[*]}; do    
    if [[ -f "$file" ]]; then
        if [[ "$file"=*[".request"] ]]; then
            # Enters here when .request is a substring.
        fi
    fi
    if [[ -d "$file" ]]; then
            # ... some logics       
    fi
done
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
Guy Sadoun
  • 427
  • 6
  • 17

1 Answers1

1

Try to use: [[ $file == *.request ]]

handle-request.sh is a helping script for checking file names.

handle-request.sh:

while IFS='' read -r line || [[ -n "$line" ]]; do
    if [[ $line == *.request ]]; then
        echo $line
    fi
done < "$1"

Explanation: reference

IFS='' (or IFS=) prevents leading/trailing whitespace from being trimmed.

-r prevents backslash escapes from being interpreted.

|| [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n (since read returns a non-zero exit code when it encounters EOF).

input file:

hello.request
.request.hello
file name with space.request

Output:

hello.request
file name with space.request
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118