简体   繁体   中英

port ksh if condition to bash shell

I have this if statement in ksh, same is not working bash. what is ksh equalent in bash?

#!/bin/ksh

if [[ "$1" = @(hello|world|shell) ]] ; then
        echo "matched"
else
        echo "not matched"
fi

Thanks SR

You can enable a subset of ksh 's extended pattern support (which, luckily for you, includes the @(...) construct :) with

 shopt -s extglob
 if [[ $1 = @(hello|world|shell) ]]; then

( bash 4.1 and later temporarily enable extended pattern support for = inside [[...]] , so the shopt command is only necessary in earlier versions of bash .)

Incidentally, you can achieve the same result with the POSIX-standard case statement.

case $1 in
    hello|world|shell) echo "matched" ;;
    * ) echo "not matched" ;;
esac

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM