bash脚本切换案例出错

时间:2016-12-06 08:34:05

标签: bash

我想创建一个简单的女巫案例,我可以根据用户提示执行功能:

echo Would you like us to perform the option: "(Y|N)"

read inPut

case $inPut in
# echoing a command encapsulated by
# backticks (``) executes the command
"Y") echo 'Starting.....'
donwload_source_code


# depending on the scenario, execute the other option
# or leave as default
"N") echo 'Stopping execution'
exit
esac

但是当我执行脚本时,我收到错误:

Would you like us to perform the option: (Y|N)
n
run.sh: line 27: syntax error near unexpected token `)'
run.sh: line 27: `"N") echo 'Stopping execution''
EMP-SOF-LT099:Genesis Plamen$ 
道你知道我怎么解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

添加;;

#!/bin/bash
echo Would you like us to perform the option: "(Y|N)"

read inPut

case $inPut in
# echoing a command encapsulated by
# backticks (``) executes the command
"Y") echo 'Starting.....'
donwload_source_code


# depending on the scenario, execute the other option
# or leave as default
;;
"N") echo 'Stopping execution'
exit
;;
esac

答案 1 :(得分:2)

多个问题。

  1. 在每个;;构建
  2. 的末尾添加case
  3. exit命令在switch-case构造中放错了,而;;没有case。它应该在read或更高的末尾。
  4. echo有自己的选项可以为用户提示打印消息,可以避免不必要的#!/bin/bash read -p "Would you like us to perform the option: \"(Y|N)\" " inPut case $inPut in # echoing a command encapsulated by # backticks (``) executes the command "Y") echo 'Starting.....' donwload_source_code ;; # depending on the scenario, execute the other option # or leave as default "N") echo 'Stopping execution' exit ;; esac
  5. 无错误的脚本

    function CMChangeSignal(constant) {
        /** take initial value here if it  possible and pass it to constan fnt */
        var out = constant();
        // listen to it
        CodemirrorInstance.on('change',function(cMirror){
            // get value right from instance
            var newValue = cMirror.getValue();
            out.set(newValue);
        });
        return function () {
            return out;
        }
    }