优化后的版本

embedded/2024/10/9 15:18:07/

 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 &


http://www.ppmy.cn/embedded/121530.html

相关文章

资源《Arduino 扩展板5-单电机驱动》说明。

资源链接&#xff1a; Arduino 扩展板5-单电机驱动 1.文件明细&#xff1a; 2.文件内容说明 包含&#xff1a;AD工程、原理图、PCB。 3.内容展示 4.简述 该文件为PCB工程&#xff0c;采用AD做的。 该文件打板后配合Arduino使用&#xff0c;属于Arduino的扩展板。

git push 远程仓库 linux版

git push 远程仓库 为了将git本地仓库的内容push到远程仓库上&#xff0c;此处记录一下具体的过程&#xff1a; 1.进入到项目根目录下 2.将本地目录初始化为Git仓库。默认情况下&#xff0c;初始分支为main。 git init && git symbolic-ref HEAD refs/heads/main3.…

毕业设计选题:基于ssm+vue+uniapp的教学辅助小程序

开发语言&#xff1a;Java框架&#xff1a;ssmuniappJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;M…

Lab1 Xv6 and Unix utilities

Lab1 Xv6 and Unix utilities 目的是为了熟悉xv6和一些它的系统调用函数 Boot xv6(easy) 1.环境 环境我是用的vscode配置的wsl&#xff0c;系统是ubuntu 20.04。用虚拟机、云服务器都感觉差不多。 网上看到Ubuntu 22.04 版本不适用于20年的课程&#xff0c;在根据20年课程…

【深度学习基础模型】反卷积神经网络(Deconvolutional Networks, DN)详细理解并附实现代码。

【深度学习基础模型】反卷积神经网络&#xff08;Deconvolutional Networks, DN&#xff09;详细理解并附实现代码。 【深度学习基础模型】反卷积神经网络&#xff08;Deconvolutional Networks, DN&#xff09;详细理解并附实现代码。 文章目录 【深度学习基础模型】反卷积神…

基于元学习原型网络Prototypical Networks网络实现图像分类-完整代码数据-可直接运行

原型网络(Prototypical Networks)是一种常用于元学习的模型,旨在解决**少样本学习(few-shot learning)**问题。少样本学习要求模型能够在极少的样本(如 1-shot 或 5-shot)的情况下进行分类。原型网络通过计算每个类别的原型,并根据新样本与这些原型的距离进行分类。 在…

SpringBoot-Starter2.7.3自动装配Redisson升级版本运行时的问题

序言 在github上搜索redisson官方源码中的issue其他伙伴们提交的记录。 https://github.com/spring-projects/spring-data-redis/tree/main/src/main/java/org/springframework/data/redis/connection/zset 基础工程的pom文件中的依赖结构 springboot version <depende…

【高阶数据结构】深度探索二叉树进阶:二叉搜索树概念及其高效实现

高阶数据结构相关知识点可以通过点击以下链接进行学习一起加油&#xff01; 本章是高阶数据结构笔记的第一篇文章&#xff0c;将分享二叉搜索树的进阶概念及其高效实现的相关知识&#xff0c;欢迎大家阅读&#xff01; &#x1f308;个人主页&#xff1a;是店小二呀 &#x1f3…