首页 > 系统相关 >shell-对于变量的使用的一些脚本实现方式

shell-对于变量的使用的一些脚本实现方式

时间:2023-02-28 17:34:46浏览次数:37  
标签:脚本 status shell grep 变量 failed proxy fi coredns

for 循环简单实现

#!/bin/bash
cd /root/health-check/
> /tmp/bomp-check-result.json
for i in $(ls ./check*.sh)
do
       bash ./$i
done

  

通过端口判断kubelet是否存活

#!/bin/bash
nodeip=$(kubectl get nodes -o wide | awk 'NR>1 {print $6}')
kubelet_start=0
kubelet_end=0
failed_ip=""
for i in ${nodeip[@]}
do
        let kubelet_end++
        line=$(echo "" |telnet ${i} 10250 2>/dev/null |wc -l)
        if [ $line -eq 3 ] ;then
                let kubelet_start++
        else
                failed_ip="$failed_ip $i"
        fi
done
if [ ${kubelet_start} -eq ${kubelet_end} ];then
        status=true
else
        status=false
fi

  

通过状态检测服务是否存活

#!/bin/bash
#etcdIP=$(cat test.txt  |grep "ETCD_INITIAL_CLUSTER=" | grep -oE   '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
master=$(kubectl get nodes -o wide|grep master |awk 'NR>1{print $6}')
old=0
new=1
apiserver_failed=""
scheduler_failed=""
controller_failed=""
proxy_failed=""
for i in ${master[@]}
do
        sta=$(kubectl get pods -o wide -n kube-system  |grep kube-apiserver |grep $i  |awk '{print $3}')
        if [ $sta == "Running" ];then
                apiserver_status=true
        else
                apiserver_status=false
                apiserver_failed="$apiserver_failed $i"
        fi
        sta_sch=$(kubectl get pods -o wide -n kube-system  |grep scheduler  |grep $i  |awk '{print $3}')
        if [ $sta_sch == "Running" ];then
                scheduler_status=true
        else
                scheduler_status=false
                scheduler_failed="$scheduler_failed $i"
        fi
        sta_contro=$(kubectl get pods -o wide -n kube-system  |grep controller-manager   |grep $i  |awk '{print $3}')
        if [ $sta_contro == "Running" ];then
                controller_status=true
        else
                controller_status=false
                controller_failed="$controller_failed $i"
        fi
done
sta_proxy=$(kubectl get pods -o wide -n kube-system  |grep kube-proxy    |awk '{print $3}')
proxy_old=0
proxy_new=0
for i  in ${sta_proxy[@]}
do
        let proxy_old++
        if [ $i == "Running" ];then
                let proxy_new++
        else
                proxy_failed="$proxy_failed $i"
        fi
done
if [ $proxy_old -eq $proxy_new ];then
        proxy_status=true
else
        proxy_status=false
fi


coredns_status=$(kubectl get pods -o wide -n kube-system  |grep coredns |awk '{print $3}')
coredns_old=0
coredns_new=0
for i in ${coredns_status[@]}
do
        let coredns_old++
        if [ $i == "Running" ];then
                let coredns_new++
        fi
done
if [ $coredns_old -eq $coredns_new ];then
        coredns_status=true
else
        coredns_status=false
fi

  

shell脚本多个选项的实现方式 (指定开关)

#!/bin/sh
set -o pipefail
red(){
        echo -e "\033[31m \t$1 \033[0m"

}
green(){
        echo -e "\033[32m \t$1 \033[0m"
}
yellow(){
        echo -e "\033[33m \t$1 \033[0m"
}
blue(){
        echo -e "\033[34m $1 \033[0m"
}
usage()
{
    cat << EOF
Rolling upgrade kernel

Usage : $(basename $0) -v <version>  -f <yes>
      -h | --help         : Show this message
      -v | --version      : Specify kernel version
      -y | --yes          : Upgrade confirmation switch
Example :
      $(basename $0) -v 406 -y yes
EOF
}

while (($#)); do
    case "$1" in
        -h | --help)
                        usage;   exit 0
                        ;;
        -v | --verison)
                        value=${2}; shift 2
                        ;;
        -y | --yes)
                        switch=${2}; shift 2
                        ;;
        *)
            usage
            echo "ERROR : Unknown option"
            exit 3
        ;;
    esac
done

if [[ ${switch} != "yes" ]];then
        red "Please turn on the switch,View help through -h or --help"
        exit 5
fi

  

 

标签:脚本,status,shell,grep,变量,failed,proxy,fi,coredns
From: https://www.cnblogs.com/gsda/p/17165269.html

相关文章

  • linux 环境启动jar服务, 启动脚本详细解释
    掰碎了讲一个jar包启动文件.当前服务目录如下#ls/easy/test/info/->binmytest.jar#ls/easy/test/info/bin/->spring-boot.shspring-boot.sh文件内容如......
  • 同步脚本
    #!/bin/bash#1.判断参数个数if[$#-lt1]thenechoNotEnoughArguement!exit;fi#2.遍历集群所有机器rpm-qrsync&>/dev/null||yuminstall-y......
  • PostgreSQL备份脚本
    1.直接上代码#!/bin/bash#数据库名称db_name="odoo14e"#存放备份文件的url地址base_url="/home/odoo/pgsql_backup/backup_files/"#判断是否存在存放备份文......
  • Mac 系统中,因环境变量配置错误,导致终端命令不可用,解决方法记录
    背景  在安装Auller过程中,因为第一次在mac系统中配置环境变量,环境变量配置错误,导致在终端中无法使用ls命令,显示无法识别此命令。现象:执行ls语句,查询当前目录......
  • [Shell] Windows上支持Linux Shell的工具/方法
    0概述1方式一:WindowsTerminal安装方式安装WindowsTerminal的最简单方法是通过MicrosoftStore:ms-windows-store://pdp/?ProductId=9n0dx20hk701如果此链接......
  • shell中产生随机字符串的方法
    random变量echo$RANDOM8746生成0-32767之间的整数随机数,若超过5位可以加个固定10位整数,然后进行求余。再结合md5生成字符串echo$RANDOM|md5sum|head-c10e0......
  • 免登陆,用脚本提前修改微信、企业微信、QQ的聊天路径
    为什么要改聊天路径微信、企业微信、QQ的聊天记录默认都是放在登陆用户的文档目录下面的登陆用户的文档目录路径C:\Users\%USERNAME%\Documents在我的日常工作中,碰到......
  • 写一个linux 定时器与jar的启动脚本
    1.在etc/crontab写定时执行的脚本SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root#Fordetailsseeman4crontabs#Exampleofjobdefinition:......
  • Linux 经典脚本
    编写helloworld脚本#!/bin/bash#编写helloworld脚本echo"HelloWorld!"通过位置变量创建Linux系统账户及密码#!/bin/bash#通过位置变量创建Linux系......
  • shell-awk-打印进程ID
    dockerps|grepcita|awk-F""'{print$1}'awk-F""按空格分割e.g.[root@host-10-0-169-67~]#sudodockerpsCONTAINERIDIMAGE......