简体   繁体   English

从bash中的case语句调用时,命令未执行

[英]command not executing when called from a case statement in bash

I'm stuck and trying to troubleshoot why the command within the case statement will not execute within this bash script. 我陷入困境,并试图解决为什么case语句中的命令无法在此bash脚本中执行的问题。

If I copy and paste the command into the command line and hardcode the value it will work. 如果我将命令复制并粘贴到命令行中并对其进行硬编码,它将起作用。 Example: psql -h mycluster-1.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="foo_bar" -f getTableDetails.sql 示例: psql -h mycluster-1.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="foo_bar" -f getTableDetails.sql

In the script, I encased the command within parentheses for possible whitespace issues: 在脚本中,我将命令括在括号内以解决可能的空白问题:

    #!/usr/local/bin/bash

    set -e
    set -x


    cluster_to_endpoint() {
      case "$1" in
        c01)
          RUNCMD=$(psql -h mycluster-1.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="$NAME" -f getTableDetails.sql)
          ;;
        c02)
          RUNCMD=$(psql -h mycluster-2.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="$NAME" -f getTableDetails.sql)
          ;;
      esac
    }


    while getopts ":c:n:" opt; do
      case $opt in
        c) cluster="$OPTARG";;
        n) name="$OPTARG";;
        *) usage
           exit 1
           ;;
      esac
    done

I am executing the script like so: ./myscript.sh -c c01 -n foo_bar 我正在像这样执行脚本: ./myscript.sh -c c01 -n foo_bar

I've also tried just removing the () as well: 我也尝试过只删除()

c01) 
   psql -h mycluster-2.us-east-1.redshift.amazonaws.com -U masteruser -d dev -p 5439 -v v1="$NAME" -f getTableDetails.sql;;

It looks like the variables are set. 看起来变量已设置。

$ ./myscript.sh -c c01 -n foo_bar
+ getopts :c:n: opt
+ case $opt in
+ cluster=c01
+ getopts :c:n: opt
+ case $opt in
+ name=foo_bar
+ getopts :c:n: opt

So why isn't the command executing? 那为什么命令不执行呢? Thanks. 谢谢。

未执行的命令位于函数cluster_to_endpoint ,该函数从未调用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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