網(wǎng)站首頁 編程語言 正文
查看日志:
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為回車符,所以導(dǎo)致每行的配置都多了個\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
相關(guān)推薦
- 2023-01-29 Python?find()、rfind()方法及作用_python
- 2022-10-14 wget -c 斷點續(xù)傳命令
- 2022-02-24 antv/g2圖表tooltip自定義并展示坐標(biāo)之外的數(shù)據(jù)
- 2022-09-15 python?Pandas庫read_excel()參數(shù)實例詳解_python
- 2021-11-08 深入解析golang中的標(biāo)準(zhǔn)庫flag_Golang
- 2023-03-19 Android大作業(yè)功能設(shè)計之自動登錄和記住密碼_Android
- 2022-02-01 Axure谷歌瀏覽器Chrome擴(kuò)展程序下載及安裝方法
- 2022-05-20 ElasticSearch 7.X系列之:查詢分析索引磁盤使用空間_disk_usage
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支