【问题标题】:Prevent zsh from adding literal quotes to command that is run from bash script防止 zsh 将文字引号添加到从 bash 脚本运行的命令
【发布时间】:2021-05-24 16:37:42
【问题描述】:

我有一个 bash 脚本,它将安装在一些同事的机器上,以防止他们在生产中运行具有潜在危险的东西。对于以dbt 开头的命令,它将运行dbt_safety_filter() 函数。

重要的部分是command+=" --exclude tag:subject_to_qa",这是将保护措施附加到他们的命令的部分。

由于某种原因,此命令的输出出错,因为它在附加部分周围添加了引号。调试输出为:

+ dbt_filter.sh:95 @dbt_safety_filter:10> dbt run --models bcnc_pqr_breast_cancer_screening ' --exclude tag:subject_to_qa'

特别是dbt run --models bcnc_pqr_breast_cancer_screening ' --exclude tag:subject_to_qa' 的问题是在命令的最后部分添加引号,这反过来又破坏了命令

  1. 为什么会这样?
  2. 如何防止将文字引号添加到命令末尾?
#!/bin/bash

function dbt_safety_filter {

  if [[ "$AWS_ENVIRONMENT" = "production" ]]; then  

      local command=("$@")
      command+=" --exclude tag:subject_to_qa"
        
      echo '\nProceeding on production on latest clean master.'
      echo '\n-----------------------------------------------'
      set -x 
      "`whence -p dbt`" $command
      set +x
    fi

}

alias dbt='dbt_safety_filter'

【问题讨论】:

  • unset -x 无效,应该出错。你确定你运行的是 bash,而不是 zsh?
  • @KamilCuk 它确实出错了,你是对的。但它会在它之前打印我的调试行。只是还没修好。将编辑。
  • 试试command+=( --exclude tag:subject_to_qa )
  • 然后您需要使用"$(whence -p dbt)" "${command[@]}",除非您实际上并未使用bash。 (whence 是一个zsh 命令,zsh 允许您使用$foo 代替${foo[@]}
  • Shellcheck 发现代码有几个问题。此外,whence 不是 Bash 命令。等效的 Bash 是 type,但在 Bash 中最好的选择是使用 command dbt "${command[@]}"

标签: bash zsh


【解决方案1】:

您的具体问题的答案是您将 --x y 添加到您的参数列表中,这当然是一个错误,因为您实际上需要两个单独的参数--xy。要纠正它,请使用:

command+=( --exclude tag:subject_to_qa )

您的代码可能还有其他问题,我没有检查。

【讨论】:

    猜你喜欢
    • 2010-09-19
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2013-01-21
    相关资源
    最近更新 更多