不同类型的值

控制流 / 条件语句 / switch/case 语句

#+( ) 是 extglob 的模式:括号内的字符出现一次或多次
shopt -s extglob

#bash 没有类型,每个值都是字符串:
#case 自上而下地把它与文件名模式逐一比较
describe() {
    case $1 in
        2)
            echo "two"
            ;;
        "Error")
            echo "Error string"
            ;;
        ?(-)+([0-9]))
            echo "integer"
            ;;
        *.txt)
            echo "text file name"
            ;;
        "")
            echo "empty string"
            ;;
        *)
            echo "string value '$1'"
            ;;
    esac
}

result1=$(describe 2)
#result1 is "two"
result2=$(describe -15)
#result2 is "integer"
result3=$(describe "notes.txt")
#result3 is "text file name"
result4=$(describe "test")
#result4 is "string value 'test'"

echo "result1 is $result1"
echo "result2 is $result2"
echo "result3 is $result3"
echo "result4 is $result4"