Shell脚本

103 阅读1分钟

当前时间

now_date=`date "+%Y%m%d-%H%M%S"`

判断目录是否存在

if [ ! -d "$mysql_bak_dir" ]; then
        mkdir -p $mysql_bak_dir
fi

判断文件不为空

if [[ -s "$load_images_pwd/images-output.txt" ]];then   # 文件存在且不为空
fi

判断返回值

if [ $? -ne 0 ];then
    echo -e "\e[31m Git Pull error,exit! \e[0m"
    exit 1
fi

镜像load_and_push.sh

[root@k8s-master01 images-packs]# cat load_and_push.sh 

#!/bin/bash

# 自定义变量
# 镜像目录
load_images_pwd=`pwd`
# 实际镜像仓库地址
push_new_registry="images.xxxxxx.com"
# 实际镜像项目名
push_new_project="devops"

load(){
    for i in `ls $load_images_pwd | grep tar`;
    do
	docker load -i $load_images_pwd/$i >> $load_images_pwd/images-output.txt
    done
}

push(){
    images_list=`cat $load_images_pwd/images-output.txt`
    for i in $images_list;
    do
	num=`echo $i | awk -F "/" '{print NF-1}'`
        if [ $num -eq 0 ]; then
	    image_new_tag=$i
        elif [ $num -eq 1 ]; then
	    image_new_tag=`echo $i | awk -F "/" '{print $2}'`
        elif [ $num -eq 2 ]; then
            image_new_tag=`echo $i | awk -F "/" '{print $3}'`
        else
            echo "No available images"
	    exit 1;
        fi
	docker tag $i $push_new_registry/$push_new_project/$image_new_tag
	docker push $push_new_registry/$push_new_project/$image_new_tag
    done
}

if [ -f "$load_images_pwd/images-output.txt" ]; then
        rm -f "$load_images_pwd/images-output.txt"
fi

load

if [[ -s "$load_images_pwd/images-output.txt" ]]   # 文件存在且不为空
then
    sed -i "s/Loaded image: //g" $load_images_pwd/images-output.txt
else
    echo Dir is empty,Exit!
    exit 1;
fi

push