Home > database >  Unterminated quoted string error while running 'kubectl exec' using shell
Unterminated quoted string error while running 'kubectl exec' using shell

Time:02-02

I am trying to run a kubectl exec command to run the command in the respective containers and transfer their outputs to a file using a shell script. I have the data in a YAML file with name of container, pod name, command and the file name where it is to be stored. I have parsed the YAML file using yq package and am trying to execute the commands. The commands having no quotes are executed successfully but the ones containing quotes result in an error. Collect execs contain name of the file and command.

I have tried running the commands normally on the command line and they seem to execute without any error. The error comes when I store them in a variable and then execute them.

Also doesn't work if I use " or ' or change ' to ".

FUNCTION

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 FILE STRUCTURE:

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

ERROR

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

CodePudding user response:

The commands running with sh -c are causing the errors. To fix this, I changed the YAML structure and broke up the commands into 2 parts: precmd and 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'

And then run the check:

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

This seems to be working for me.

  • Related