If condition not returning desired output in unix ksh - returns wrong output If condition not returning desired output in unix ksh - returns wrong output unix unix

If condition not returning desired output in unix ksh - returns wrong output


(( )) is for integer arithmetic. All your strings evaluate to zero numerically and zeros are equal to zeros, hence the positive result of the test.

You can do either:

if  [ "$fileext" = "txt" ]  ||  [ "$fileext" = "csv" ]  ||  [ "$fileext" = "zip" ]  ||  [ "$fileext" = "*" ] then    echo "$fileext is a proper file extension"else        echo "$fileext is not an appropriate file extension"fi

or

case "$fileext" in     txt|csv|zip|"*")         echo "$fileext is a proper file extension"        ;;        *)         echo "$fileext is not an appropriate file extension"        ;;esac

(These snippets should additionally be POSIX and therefore not require special shells such as ksh.)


I'm guessing you don't want the asterisk treated as a wildcard character but as a single character asterisk ('*'). If so, you should be able to use the same ksh solution I provided for your compare a substring of variable with another string in unix post:

fileext=${1##*.}if [[ "${fileext}" = @(txt|zip|csv|\*) ]]; then    echo "$fileext is a proper file extension"else    echo "$fileext is not an appropriate file extension"fi

The '@' is a multi-pattern matching construct. In this case it's asking for an exact match of strings 'txt', 'zip', 'csv' or the asterisk as a character '*'.

The asterisk has to be escaped otherwise it's treated as a wildcard character, eg:

if [[ "${fileext}" = @(*txt*) ]] ...

will match 'txt', 'abctxt', 'txtdef', 'abctxtdef'

NOTE: See PSkocik's answer for a POSIX/non-ksh solution.