日期:2014-05-16  浏览次数:20872 次

shell老报错,为什么?
想做个类似于Makefile作用的shell脚本,当./comile.sh remove时可以删除除.c与.sh以外的所有文件,但为什么老出错,错误为:
./compile.sh: line 10: 在未预料的“(”附近出现语法错误
./compile.sh: line 10: ` ls !(*.c|*.sh) | xargs rm'

代码为:
C/C++ code
#read -p "which files are what you want to compile? "
case $# in
    "1")
        if [ "$1" == "remove" ]; then
            ls !(*.c|*.sh) | xargs rm
            
            if [ "$?" == "0" ]; then
                echo "remove successfully"
            else
                echo "remove failure"
            fi
        else
            gcc -o ${1%.c} $1
        fi
        ;;
    "2")
        gcc -o ${1%.c} $1
        gcc -o ${2%.c} $2
        ;;
    *)
        echo "usage: $0 para1 [para2 ....]"
        ;;
esac



------解决方案--------------------
楼主的用法是bash的扩展,需要开启extglob选项。

开头加上shopt -s extglob