Linux拷贝文件到容器中进行替换的shell脚本

news/2024/9/19 15:35:41

vi pushJar2docker.sh 

#!/bin/sh

# Linux拷贝文件到容器中进行替换的shell脚本

argc=$#
newline=$'\n'
push_file=app-core-1.0-SNAPSHOT.jar
target_dir=/opt/app/lib
docker_id=0
# eg: test_module01-v1.jar
push_file_check_regex='^[a-zA-Z0-9._-]+\.jar$'
# eg: /opt/app/lib 
target_dir_check_regex='^[a-zA-Z0-9_/-]+$'


# functions define

function get_docker_id()
{
  # return docker_id
  docker_id=$(docker ps -a |grep -E ".*hub.center.tech/app/core.*user01.*" |awk '{print $1}')
  #return docker_id  # return will return result: 0|1
  echo $docker_id    # echo for return docker_id
}

function copy_file_to_docker(){
  # copy default jar file to default target_dir
  docker_id=$(get_docker_id)
  echo "docker id: $docker_id"
  
  # $1=$push_file, $2=$target_dir
  path_src=$1
  path_dst=$2
  cmd_cp="docker cp $path_src  $docker_id:$path_dst"
  echo -e "\n$cmd_cp"

  #copy file to docker
  ret=`$cmd_cp`
  echo "jar file pushed to docker finished!"

  #ls -l
  cmd_ls="docker exec $docker_id ls -l $path_dst/$path_src"
  echo -e "\n$cmd_ls"

  echo "target file properies in docker is: "
  ret=`$cmd_ls`
  echo -e "\n$ret\n"

  echo -e "Please exec by manual:\ndocker exec -it $docker_id /bin/sh \n./restart.sh\n"
  echo -e "\n --------END-----------\n"
 
  return 0
}

# body 
echo ----
echo $argc
if [ $argc -eq 1 ] && [ $1 == "-h" -o $1 == "/?" ]
then
  echo "----------------------------------------------------------------------------------"
  echo "$newline"
  echo "1.show help info:$newline Use: $0 {-h | /?} $newline"
  echo "2.direct copy default jar file $push_file to default target lib dir $target_dir:$newline Use: $0 $newline"
  echo "3.copy jar file to target lib dir:$newline Use: $0 {file} {target_dir} $newline Tips: must end with "/"$newline  eg: $0 app-core-1.0-SNAPSHOT.jar  /opt/app/lib/ $newline"
  exit
elif [ $argc -eq 0 ]
then
  # for 2.direct copy default
  echo "direct copy default"
  copy_file_to_docker $push_file $target_dir
elif [ $argc -eq 2 ] && [[ $1 =~ $push_file_check_regex ]] && [[ $2 =~ $target_dir_check_regex ]]
then
  # for 3.copy jar file to target lib dir
  echo "copy jar file to target lib dir"
  push_file=$1
  target_dir=$2
  copy_file_to_docker $push_file $target_dir
else
  echo -e "Input error!\nUse: $0 -h for help!"
  if [ $argc -ne 2 ]
  then
    echo "input argc number invalid!"
  elif [[ ! $1 =~ $push_file_check_regex ]]
  then
    echo "input push_file invalid: \"$1\" not match: \"$push_file_check_regex\""
  elif [[ ! $2 =~ $target_dir_check_regex ]]
  then
    echo "input target_dir invalid: \"$2\" not match: \"$target_dir_check_regex\""
  else
    echo "unknow error!"
  fi
  exit
fi
 


http://lihuaxi.xjx100.cn/news/1142740.html

相关文章

表格数据中文展示

模板 <el-table-columnprop"authority"label"权限"width"220" ><template slot-scope"scope"><el-tag v-for"item in scope.row.authority" :key"item"><!-- {{ item }} -->{{ translat…

吃透阿里2023版Java性能优化小册后,我让公司系统性能提升了200%

性能优化可以说是很多一线大厂对其公司内高级开发的基本要求 &#xff08;其中以Java岗最为显著&#xff09; 。其原因有两个&#xff1a;一是提高系统的性能&#xff0c;二是为公司节省资源。两者都能做到&#xff0c;那你就不可谓不是普通程序员眼中的“调优大神了”。 那么…

Matlab入门教程003|MATLAB变量|MATLAB命令

MATLAB变量 每个MATLAB变量可以是数组或者矩阵。 用一个简单的方法指定变量。例如&#xff1a; x 3 % defining x and initializing it with a value MATLAB执行上述语句&#xff0c;并返回以下结果&#xff1a; x 3 上述的例子创建了一个1-1的矩阵名为x和的值存储…

数据结构及算法应用

第十四章、数据结构及算法应用 1、分治法 对于一个规模为n的问题&#xff0c;若该问题可以容易地解决&#xff08;比如说规模n较小&#xff09;则直接解决&#xff1b;否则将其分解为k个规模较小的子问题&#xff0c;这些子问题互相独立且与原问题形式相同&#xff0c;递归地…

web自动化测试进阶篇02 ——— BDD与TDD的研究实践

&#x1f60f;作者简介&#xff1a;博主是一位测试管理者&#xff0c;同时也是一名对外企业兼职讲师。 &#x1f4e1;主页地址&#xff1a;【Austin_zhai】 &#x1f646;目的与景愿&#xff1a;旨在于能帮助更多的测试行业人员提升软硬技能&#xff0c;分享行业相关最新信息。…

260道网络安全常见面试题汇总(附答案解析+配套资料)

以下为网络安全各个方向涉及的面试题&#xff0c;星数越多代表问题出现的几率越大&#xff0c;祝各位都能找到满意的工作。 注&#xff1a;所有的资料都整理成了PDF&#xff0c;面试题和答案将会持续更新&#xff0c;因为无论如何也不可能覆盖所有的面试题。 目录 一、渗透测试…

PowerShell Install 二进制方式部署Python3

Python 前言 Python 是一种解释型、面向对象、动态数据类型的高级程序设计语言。Python 由 Guido van Rossum 于 1989 年底发明&#xff0c;第一个公开发行版发行于 1991 年。像 Perl 语言一样, Python 源代码同样遵循 GPL(GNU General Public License) 协议。 Python downlo…

IM即时通讯系统[SpringBoot+Netty]——梳理(五)

文章目录 十一、打造QQ在线状态功能之为你的应用增添色彩1、在线状态设计2、Netty网关用户状态变更通知、登录ack3、逻辑层处理用户上线下线4、在线状态订阅—临时订阅5、实现手动设置客户端状态接口6、推拉结合实现在线状态更新 十二、IM扩展—能做的事情还有很多1、如何让陌生…