【问题标题】:Unterminated quoted string error while running 'kubectl exec' using shell使用 shell 运行 \'kubectl exec\' 时出现未终止的引用字符串错误
【发布时间】:2023-02-02 02:15:32
【问题描述】:

我正在尝试运行 kubectl exec 命令以在各自的容器中运行命令,并使用 shell 脚本将它们的输出传输到文件。我将数据保存在一个 YAML 文件中,其中包含容器名称、pod 名称、命令和要存储的文件名。我已经使用 yq 包解析了 YAML 文件并尝试执行命令。没有引号的命令执行成功,但包含引号的命令会导致错误。 Collect execs 包含文件名和命令。

我已经尝试在命令行上正常运行这些命令,它们似乎没有任何错误地执行。当我将它们存储在一个变量中然后执行它们时,就会出现错误。

如果我使用 " 或 ' 或将 ' 更改为 ",也不起作用。

功能

get_execs() {
    mkdir ${EXECDIR}
    for con in $(yq '.containers[] | .name' ${YFILE})
    do
        # echo $con
        x=$(i=$con yq '.containers[] | select(.name == env(i)) | .collect_execs[] | .name' ${YFILE})
        # printf "%s\n" "$x"
        mkdir ${EXECDIR}/$con
        for j in $x
        do
            c=$(i=$con p=$j yq '.containers[] | select(.name == env(i)) | .collect_execs[] | select(.name == env(p)) | .cmd' ${YFILE})
            pod=$(i=$con yq '.containers[] | select(.name == env(i)) | .pod' ${YFILE})
            # printf "%s abc\n" "$c"
            kubectl exec -n ${NAMESPACE} $pod -c $con -- $c > ${EXECDIR}/$con/$j
        done
    done
}

YAML 文件结构:

containers:
  - name: otg-port-eth1-protocol-engine
    pod: otg-port-eth1
    collect_execs:
    - name: resource-usage
      cmd: top -c -n 2 -b -H -w 120
    - name : disk-space
      cmd : df -H 
    - name: cpu-info
      cmd: cat /proc/cpuinfo
    - name: interface-manager-threads-iter1
      cmd : sh -c 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
    - name: interface-manager-threads-iter2
      cmd : sh -c 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
    - name: interface-manager-shared-sos
      cmd: sh -c 'cat /proc/$(pidof InterfaceManager)/maps'
    - name: netstat
      cmd: netstat -an
    - name: dmesg
      cmd : dmesg
    - name : ifconfig
      cmd : ifconfig

错误

--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
/proc/$(pidof: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
--eval-command: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2
/proc/$(pidof: 1: Syntax error: Unterminated quoted string
command terminated with exit code 2

【问题讨论】:

    标签: shell ubuntu yaml exec kubectl


    【解决方案1】:

    使用 sh -c 运行的命令导致错误。为了解决这个问题,我更改了 YAML 结构并将命令分为两部分:precmd 和 cmd。

    - name: resource-usage
      cmd: top -c -n 2 -b -H -w 120
    - name : disk-space
      cmd : df -H 
    - name: cpu-info
      cmd: cat /proc/cpuinfo
    - name: interface-manager-threads-iter1
      precmd : sh -c
      cmd : 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
    - name: interface-manager-threads-iter2
      precmd : sh -c
      cmd : 'gdb --eval-command "set pagination 0" --eval-command "thread apply all bt" --batch --pid $(pidof InterfaceManager)'
    - name: interface-manager-shared-sos
      precmd : sh -c
      cmd: 'cat /proc/$(pidof InterfaceManager)/maps'
    

    然后运行检查:

    if [ ! "$pc" = null ]
    then
        kubectl exec -n ${NAMESPACE} $pod -c $con -- $pc "$c" > ${EXECDIR}/$con/$j
    else
        kubectl exec -n ${NAMESPACE} $pod -c $con -- $c > ${EXECDIR}/$con/$j
    fi
    

    这似乎对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多