ubuntu 使用shell运行'kubectl exec'时出现未终止的引号字符串错误

bvjveswy  于 2023-02-03  发布在  Shell
关注(0)|答案(1)|浏览(324)

我正在尝试运行kubectl exec命令,以便在相应的容器中运行该命令,并使用shell脚本将其输出传输到一个文件中。命令和要存储它的文件名。我已经使用yq包解析了YAML文件,并正在尝试执行命令。不带引号的命令会成功执行,但带引号的命令会导致错误。收集包含文件名和命令的execs。
我试过在命令行上正常运行这些命令,它们似乎执行时没有任何错误。当我将它们存储在变量中然后执行它们时,错误出现了。
如果使用“or”或将“更改为“,也不起作用。

功能

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
lndjwyie

lndjwyie1#

使用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

这似乎对我有用。

相关问题