日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

shell腳本查看k8s日志介紹_linux shell

作者:小小小牛牛 ? 更新時間: 2022-03-24 編程語言

查看日志:

kubectl logs -f podName --tail 100

比如我們?nèi)绻氩橹付ǖ膒od,指定行數(shù),指定的內(nèi)容,
每次都需要輸入

kubectl logs -f xxx --tail yyy | grep zzz

為了方便,可自定義腳本,輸入

sh .sh xxx yyy zzz

即可,并且xxx支持RE;

占位符的方式

#!/bin/bash
# kubectl get pods
#notification
x="kubectl logs -f"
y="--tail"
g="|grep"
name=`kubectl get pods | grep ^$1 | awk '{print $1}'`
x="eval $x $name $y $2 $g $3"
${x}

# sh log.sh podName 20 content
# 最終:kubectl logs -f podName --tail 20 | grep content

指定參數(shù) getopts

#!/bin/bash
# ":":如果某個選項(option)后面出現(xiàn)了冒號(":"),則表示這個選項后面可以接參數(shù)
x="kubectl logs -f"
y="--tail"
g="|grep"
while getopts ":n:f:c:" opt
do
    case $opt in
        n)
		name=`kubectl get pods | grep ^$OPTARG | awk '{print $1}'`
		x="$x $name"
        ;;
        f)
		x="$x $y $OPTARG"
        ;;
        c) 
        x="$x $g $OPTARG"
        ;;
        ?)
        echo "未知參數(shù)"
        exit 1;;
    esac
done
x="eval $x"
${x}
# sh log.sh -n podName -f 20 -c content
# 最終:kubectl logs -f podName --tail 20 | grep content

問題

1.執(zhí)行 shell 腳本\r問題

腳本是在window下編輯完成后上傳到linux上執(zhí)行的,win下的換行是回車符+換行符,也就是\r\n,而unix下是換行符\n。linux下不識別\r為回車符,所以導致每行的配置都多了個\r,因此是腳本編碼的問題。

在這里插入圖片描述

2.命令中的grep

在這里插入圖片描述

可以發(fā)現(xiàn)最終拼接出來的字符串,是一條正確的命令,但是通過${CMD}執(zhí)行該變量報錯。

原因:
如果在shell中定義一個命令,帶了管道,例如

CMD=“l(fā)s -l | grep xx”

直接執(zhí)行$CMD,會出現(xiàn)如下報錯

ls: cannot access |: No such file or directory

ls: cannot access grep: No such file or directory

管道符會被解釋為普通字符

加上eval

CMD=“eval ls -l | grep xx”

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/qq_45071180/article/details/122318501

欄目分類
最近更新