当前位置: 首页 > news >正文

优化后的版本

 docker_operations.sh

#!/bin/bash# all definition
NETWORK_NAME="net-1"
VOLUME_MOUNT="-v /home/norten/Public/tools:/mnt" # 容器内部挂载到主机的路径
SCRIPT_ROUTE="/mnt" # container_run_medium.sh所在的路径
IMAGE_NAME="ubuntu"# View help command
function help_container() {echo " Usage: ./docker_operations.sh start 1 10"echo " "echo " create [num] "echo " start [start_num] [end_num] "echo " exec [start_num] [end_num] "echo " entry [num] "echo " stop [start_num] [end_num] "echo " remove [num] " echo " info [num]"echo " check [start_num] [end_num]"echo " "echo " Usage: exit "echo " exit <exit the container>"echo " docker ps  <view all running containers>"echo " docker ps -a  <view all containers>"echo " "
}# Dynamic container creation
function create_container() {echo "create zero paremeter is: $0"  echo "create first paremeter is: $1"echo "create second paremeter is: $2"  local num="$1"local CONTAINER_IP="192.168.0.$((num+60))"echo "IP IS $CONTAINER_IP"local CONTAINER_NAME="container-$num"# Check whether the IP address is already in uselocal existing_ips=($(docker inspect --format='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) 2>/dev/null))for ip in "${existing_ips[@]}"; doif [[ "$ip" == "$CONTAINER_IP" ]]; thenecho "Error: IP Address $CONTAINER_IP is already in use by another container."exit 1fidone  # Trying to create a containerdocker run -itd \--name "$CONTAINER_NAME" \--network="$NETWORK_NAME" \--ip="$CONTAINER_IP" \$VOLUME_MOUNT \$IMAGE_NAME \&& echo "Container $CONTAINER_NAME created with IP $CONTAINER_IP." \|| { echo "Failed to create container $CONTAINER_NAME."; exit 1; }}# Start specified or a range of containers
function start_container() {echo "start zero paremeter is: $0"  echo "start first paremeter is: $1"echo "start second paremeter is: $2"local start_num="$1"local end_num="${2:-$start_num}"  # If the second argument is not provided, it defaults to the value of the first argumentfor (( i=start_num; i<=end_num; i++ )); dolocal CONTAINER_NAME="container-$i"if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; thenecho "Starting container $CONTAINER_NAME..."docker start "$CONTAINER_NAME"echo "Container $CONTAINER_NAME started."elseecho "Error: Container $CONTAINER_NAME does not exist."exit 1fidone
}# Stop specified or a range of containers
function stop_container() {local start_num="$1"local end_num="${2:-$start_num}"  # If the second argument is not provided, it defaults to the value of the first argumentfor (( i=start_num; i<=end_num; i++ )); dolocal CONTAINER_NAME="container-$i"if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; thenecho "Stopping container $CONTAINER_NAME..."docker stop "$CONTAINER_NAME"echo "Container $CONTAINER_NAME stopped."elseecho "Warning: Container $CONTAINER_NAME does not exist."fidone
}# Enter the shell of a specified container or range of containers
function exec_container() {local start_num="$1"local end_num="${2:-$start_num}"  # If the second argument is not provided, it defaults to the value of the first argumentfor (( i=start_num; i<=end_num; i++ )); dolocal CONTAINER_NAME="container-$i"if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; thenecho "Executing script in container $CONTAINER_NAME..."# Enter the container and run the scriptdocker exec -d "$CONTAINER_NAME" bash -c "cd $SCRIPT_ROUTE && ./container_run_medium.sh"echo "Script executed in container $CONTAINER_NAME."# Wait for a short time to ensure the process startssleep 2elseecho "Error: Container $CONTAINER_NAME does not exist or is not running."exit 1fidone
}# Remove a specified container
function remove_container() {local container_num="$1"local CONTAINER_NAME="container-$container_num"if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; thenecho "Removing container $CONTAINER_NAME..."docker rm -f "$CONTAINER_NAME"echo "Container $CONTAINER_NAME removed."elseecho "Error: Container $CONTAINER_NAME does not exist."exit 1fi
}# Function to display information about a specified container
function info_container() {local container_num="$1"if ! [[ "$container_num" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number must be an integer between 1 and 1000."return 1filocal CONTAINER_NAME="container-$container_num"# Check if the container existsif docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q 'true'; thenecho "Information for container $CONTAINER_NAME:"docker inspect "$CONTAINER_NAME"elseecho "Error: Container $CONTAINER_NAME does not exist or is not running."fi
}# Check if the script is running in a specified container or range of containers
function check_script_running() {local start_num="$1"local end_num="${2:-$start_num}"  # If the second argument is not provided, it defaults to the value of the first argumentfor (( i=start_num; i<=end_num; i++ )); dolocal CONTAINER_NAME="container-$i"# Check if the container exists and is runningif docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q 'true'; thenecho "Container $CONTAINER_NAME is running."# Check if MediumBoxBase is running using pgrepif docker exec -it "$CONTAINER_NAME" pgrep -f 'MediumBoxBase' > /dev/null; thenecho "Script is running in container $CONTAINER_NAME."elseecho "Error: Script is not running in container $CONTAINER_NAME."fielseecho "Error: Container $CONTAINER_NAME does not exist or is not running."fi# Add a newline for separationechodone# Ensure the terminal ends with a newlineecho
}# Function to enter a specified container
function entry_container() {local container_num="$1"if ! [[ "$container_num" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number must be an integer between 1 and 1000."return 1filocal CONTAINER_NAME="container-$container_num"# Check if the container exists and is runningif docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q 'true'; thenecho "Entering container $CONTAINER_NAME..."docker exec -it "$CONTAINER_NAME" bashelseecho "Error: Container $CONTAINER_NAME does not exist or is not running."fi
}case "$1" inhelp)help_container;;create)if [ "$#" -ne 2 ]; thenecho "Error: You should provide a parameter after 'create'."exit 1fiif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number must be an integer between 1 and 1000."exit 1ficreate_container "$2";;start)# Check the number of parameters to determine whether to start a single container or a container rangeif [ "$#" -lt 2 ]; thenecho "Error: You should provide at least one number after 'start'."exit 1elif [ "$#" -eq 2 ]; then# If there are only two parameters, try starting a containerstart_container "$2"elif [ "$#" -eq 3 ]; then# If you have three parameters, try starting a series of containersif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]] || ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: Both numbers must be integers between 1 and 1000."exit 1fiif [ "$2" -gt "$3" ]; thenecho "Error: The first number must be less than or equal to the second."exit 1fistart_container "$2" "$3"elseecho "Error: Too many arguments for 'start'."exit 1fi;;stop)if [ "$#" -lt 2 ]; thenecho "Error: You should provide at least one number after 'stop'."exit 1fiif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number(s) must be integers between 1 and 1000."exit 1fiif [ "$#" -eq 2 ]; thenstop_container "$2"elif [ "$#" -eq 3 ]; thenif ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: Both numbers must be integers between 1 and 1000."exit 1fiif [ "$2" -gt "$3" ]; thenecho "Error: The second number must be greater than or equal to the first."exit 1fistop_container "$2" "$3"elseecho "Error: Too many arguments for 'stop'."exit 1fi;;exec)if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; thenecho "Error: You should provide one or two numbers after 'exec'."exit 1fiif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number(s) must be integers between 1 and 1000."exit 1fiif [ "$#" -eq 2 ]; thenexec_container "$2"elif [ "$#" -eq 3 ]; thenif ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: Both numbers must be integers between 1 and 1000."exit 1fiif [ "$2" -gt "$3" ]; thenecho "Error: The first number must be less than or equal to the second."exit 1fiexec_container "$2" "$3"fi;;check)if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; thenecho "Error: You should provide one or two numbers after 'check'."exit 1fiif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number(s) must be integers between 1 and 1000."exit 1fiif [ "$#" -eq 2 ]; thencheck_script_running "$2"elif [ "$#" -eq 3 ]; thenif ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: Both numbers must be integers between 1 and 1000."exit 1fiif [ "$2" -gt "$3" ]; thenecho "Error: The first number must be less than or equal to the second."exit 1ficheck_script_running "$2" "$3"fi# Reset the terminal to ensure normal behaviorstty sane;;entry)if [ "$#" -ne 2 ]; thenecho "Error: You should provide exactly one number after 'entry'."exit 1fiif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number must be an integer between 1 and 1000."exit 1fientry_container "$2";;remove)if [ "$#" -ne 2 ]; thenecho "Error: You should provide exactly one number after 'remove'."exit 1fiif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number must be an integer between 1 and 1000."exit 1firemove_container "$2";;info)if [ "$#" -ne 2 ]; thenecho "Error: You should provide exactly one number after 'info'."exit 1fiif ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; thenecho "Error: The number must be an integer between 1 and 1000."exit 1fiinfo_container "$2";;logs|status)echo "Function '$1' has not been updated to handle numbered containers."exit 1;;*)echo "Invalid command. Use './docker_operations.sh help' to get instructions."exit 1;;
esacexit 0

./mnt/container_run_medium.sh

#!/bin/bash# 清理旧的日志文件
rm -f *.log
rm -f nohup.out
rm -f cssd.dat# 启动 pwbox_simu 和 MediumBoxBase
nohup /mnt/simutools/pwbox_simu /mnt/simutools/pw_box.conf > /dev/null 2>&1 &
nohup /mnt/mediumSimu/MediumBoxBase /mnt/mediumSimu/hynn_flash_config_simu.conf > /dev/null 2>&1 &

相关文章:

  • 【Vue】vue2项目打包后部署刷新404,配置publicPath ./ 不生效问题
  • 正则表达式中的贪婪模式和非贪婪模式
  • 小程序原生-利用setData()对不同类型的数据进行增删改
  • 《微信小程序实战(4) · 地图导航功能》
  • 【每天学个新注解】Day 10 Lombok注解简解(九)—@Accessors
  • 第十三届蓝桥杯真题Java c组C.纸张尺寸(持续更新)
  • UNI-APP_iOS开发技巧之:跳转到TestFlight或者App Store
  • SQL第11课——使用子查询
  • 983. 最低票价
  • PHP读取文件内容的几种方法和函数
  • django使用笔记6--docker部署
  • 破局汽车智能化浪潮:Tire 1供应商的网络优化与升级策略
  • 在Linux中进行OpenSSH升级(编译安装在openssh目录)
  • C语言系列4——指针与数组(1)
  • 【数据库】 MongoDB 用户分配新的角色和权限
  • hexo+github搭建个人博客
  • 【刷算法】从上往下打印二叉树
  • Electron入门介绍
  • Leetcode 27 Remove Element
  • leetcode98. Validate Binary Search Tree
  • MYSQL 的 IF 函数
  • Rancher-k8s加速安装文档
  • rc-form之最单纯情况
  • vue 配置sass、scss全局变量
  • 记录一下第一次使用npm
  • 来,膜拜下android roadmap,强大的执行力
  • 力扣(LeetCode)56
  • 前端代码风格自动化系列(二)之Commitlint
  • 一个完整Java Web项目背后的密码
  • hi-nginx-1.3.4编译安装
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • ​2021半年盘点,不想你错过的重磅新书
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (1)(1.9) MSP (version 4.2)
  • (30)数组元素和与数字和的绝对差
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (C语言)球球大作战
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (二)Kafka离线安装 - Zookeeper下载及安装
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (论文阅读11/100)Fast R-CNN
  • (三)docker:Dockerfile构建容器运行jar包
  • (一)80c52学习之旅-起始篇
  • (一)项目实践-利用Appdesigner制作目标跟踪仿真软件
  • (原)Matlab的svmtrain和svmclassify
  • (转)mysql使用Navicat 导出和导入数据库
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .net 7和core版 SignalR
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET Core中的去虚