![](/img/trans.png)
[英]Syntax error: Unterminated quoted string while calling shell in Jenkins
[英]Unterminated quoted string error while running 'kubectl exec' using shell
我正在嘗試運行 kubectl exec 命令以在各自的容器中運行該命令,並使用 shell 腳本將它們的輸出傳輸到一個文件。 我在一個 YAML 文件中有數據,其中包含容器名稱、pod 名稱、命令和要存儲的文件名。 我已經使用 yq package 解析了 YAML 文件並嘗試執行命令。 沒有引號的命令執行成功,但包含引號的命令會導致錯誤。 Collect execs 包含文件名和命令。
我已經嘗試在命令行上正常運行這些命令,它們似乎沒有任何錯誤地執行。 當我將它們存儲在一個變量中然后執行它們時,就會出現錯誤。
如果我使用 " 或 ' 或將 ' 更改為 ",也不起作用。
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 文件結構:
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
使用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
這似乎對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.