优化后的版本

news/2024/10/4 6:31:42 标签: windows

 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 use
    local existing_ips=($(docker inspect --format='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) 2>/dev/null))
    for ip in "${existing_ips[@]}"; do
        if [[ "$ip" == "$CONTAINER_IP" ]]; then
            echo "Error: IP Address $CONTAINER_IP is already in use by another container."
            exit 1
        fi
    done  

    # Trying to create a container
    docker 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 argument

    for (( i=start_num; i<=end_num; i++ )); do
        local CONTAINER_NAME="container-$i"
        if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
            echo "Starting container $CONTAINER_NAME..."
            docker start "$CONTAINER_NAME"
            echo "Container $CONTAINER_NAME started."
        else
            echo "Error: Container $CONTAINER_NAME does not exist."
            exit 1
        fi
    done
}

# 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 argument

    for (( i=start_num; i<=end_num; i++ )); do
        local CONTAINER_NAME="container-$i"
        if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
            echo "Stopping container $CONTAINER_NAME..."
            docker stop "$CONTAINER_NAME"
            echo "Container $CONTAINER_NAME stopped."
        else
            echo "Warning: Container $CONTAINER_NAME does not exist."
        fi
    done
}

# 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 argument

    for (( i=start_num; i<=end_num; i++ )); do
        local CONTAINER_NAME="container-$i"
        
        if docker ps -a --format '{{.Names}}' | grep -q "^$CONTAINER_NAME\$"; then
            echo "Executing script in container $CONTAINER_NAME..."
            # Enter the container and run the script
            docker 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 starts
            sleep 2
        else
            echo "Error: Container $CONTAINER_NAME does not exist or is not running."
            exit 1
        fi
    done
}

# 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\$"; then
        echo "Removing container $CONTAINER_NAME..."
        docker rm -f "$CONTAINER_NAME"
        echo "Container $CONTAINER_NAME removed."
    else
        echo "Error: Container $CONTAINER_NAME does not exist."
        exit 1
    fi
}

# 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}$ ]]; then
        echo "Error: The number must be an integer between 1 and 1000."
        return 1
    fi
    
    local CONTAINER_NAME="container-$container_num"
    
    # Check if the container exists
    if docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q 'true'; then
        echo "Information for container $CONTAINER_NAME:"
        docker inspect "$CONTAINER_NAME"
    else
        echo "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 argument

    for (( i=start_num; i<=end_num; i++ )); do
        local CONTAINER_NAME="container-$i"
        
        # Check if the container exists and is running
        if docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q 'true'; then
            echo "Container $CONTAINER_NAME is running."
            
            # Check if MediumBoxBase is running using pgrep
            if docker exec -it "$CONTAINER_NAME" pgrep -f 'MediumBoxBase' > /dev/null; then
                echo "Script is running in container $CONTAINER_NAME."
            else
                echo "Error: Script is not running in container $CONTAINER_NAME."
            fi
        else
            echo "Error: Container $CONTAINER_NAME does not exist or is not running."
        fi
        
        # Add a newline for separation
        echo
    done
    
    # Ensure the terminal ends with a newline
    echo
}

# 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}$ ]]; then
        echo "Error: The number must be an integer between 1 and 1000."
        return 1
    fi
    
    local CONTAINER_NAME="container-$container_num"
    
    # Check if the container exists and is running
    if docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q 'true'; then
        echo "Entering container $CONTAINER_NAME..."
        docker exec -it "$CONTAINER_NAME" bash
    else
        echo "Error: Container $CONTAINER_NAME does not exist or is not running."
    fi
}

case "$1" in
    help)
            help_container
            ;;
    create)
        if [ "$#" -ne 2 ]; then
            echo "Error: You should provide a parameter after 'create'."
            exit 1
        fi
        
        if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
            echo "Error: The number must be an integer between 1 and 1000."
            exit 1
        fi
        
        create_container "$2"
        ;;
    start)
        # Check the number of parameters to determine whether to start a single container or a container range
        if [ "$#" -lt 2 ]; then
            echo "Error: You should provide at least one number after 'start'."
            exit 1
        elif [ "$#" -eq 2 ]; then
            # If there are only two parameters, try starting a container
            start_container "$2"
        elif [ "$#" -eq 3 ]; then
            # If you have three parameters, try starting a series of containers
            if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]] || ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
                echo "Error: Both numbers must be integers between 1 and 1000."
                exit 1
            fi
            if [ "$2" -gt "$3" ]; then
                echo "Error: The first number must be less than or equal to the second."
                exit 1
            fi
            start_container "$2" "$3"
        else
            echo "Error: Too many arguments for 'start'."
            exit 1
        fi
        ;;
    stop)
        if [ "$#" -lt 2 ]; then
            echo "Error: You should provide at least one number after 'stop'."
            exit 1
        fi
        
        if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
            echo "Error: The number(s) must be integers between 1 and 1000."
            exit 1
        fi
        
        if [ "$#" -eq 2 ]; then
            stop_container "$2"
        elif [ "$#" -eq 3 ]; then
            if ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
                echo "Error: Both numbers must be integers between 1 and 1000."
                exit 1
            fi
            if [ "$2" -gt "$3" ]; then
                echo "Error: The second number must be greater than or equal to the first."
                exit 1
            fi
            stop_container "$2" "$3"
        else
            echo "Error: Too many arguments for 'stop'."
            exit 1
        fi
        ;;
    exec)
        if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
            echo "Error: You should provide one or two numbers after 'exec'."
            exit 1
        fi
        
        if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
            echo "Error: The number(s) must be integers between 1 and 1000."
            exit 1
        fi
        
        if [ "$#" -eq 2 ]; then
            exec_container "$2"
        elif [ "$#" -eq 3 ]; then
            if ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
                echo "Error: Both numbers must be integers between 1 and 1000."
                exit 1
            fi
            if [ "$2" -gt "$3" ]; then
                echo "Error: The first number must be less than or equal to the second."
                exit 1
            fi
            exec_container "$2" "$3"
        fi
        ;;
    check)
        if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
            echo "Error: You should provide one or two numbers after 'check'."
            exit 1
        fi
        
        if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
            echo "Error: The number(s) must be integers between 1 and 1000."
            exit 1
        fi
        
        if [ "$#" -eq 2 ]; then
            check_script_running "$2"
        elif [ "$#" -eq 3 ]; then
            if ! [[ "$3" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
                echo "Error: Both numbers must be integers between 1 and 1000."
                exit 1
            fi
            if [ "$2" -gt "$3" ]; then
                echo "Error: The first number must be less than or equal to the second."
                exit 1
            fi
            check_script_running "$2" "$3"
        fi
        # Reset the terminal to ensure normal behavior
        stty sane
        ;;
    entry)
        if [ "$#" -ne 2 ]; then
            echo "Error: You should provide exactly one number after 'entry'."
            exit 1
        fi
        
        if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
            echo "Error: The number must be an integer between 1 and 1000."
            exit 1
        fi
        
        entry_container "$2"
        ;;
    remove)
        if [ "$#" -ne 2 ]; then
            echo "Error: You should provide exactly one number after 'remove'."
            exit 1
        fi
        
        if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
            echo "Error: The number must be an integer between 1 and 1000."
            exit 1
        fi
        
        remove_container "$2"
        ;;
    info)
        if [ "$#" -ne 2 ]; then
            echo "Error: You should provide exactly one number after 'info'."
            exit 1
        fi
        
        if ! [[ "$2" =~ ^[1-9][0-9]{0,2}|[1-9][0-9]{3}$ ]]; then
            echo "Error: The number must be an integer between 1 and 1000."
            exit 1
        fi
        
        info_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
        ;;
esac

exit 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.niftyadmin.cn/n/5689617.html

相关文章

Linux shell编程学习笔记85:fold命令——让文件瘦身塑形显示

0 引言 我们使用的电脑屏幕有宽有窄&#xff0c;我们有时候希望文件能按照我们的屏幕宽度来调整和匹配&#xff0c;这时我们可以使用fold命令。 1 fold命令 的帮助信息、功能、命令格式、选项和参数说明 1.1 fold 命令 的帮助信息 我们可以输入命令 fold--help 来查看fold …

爬虫——同步与异步加载

一、同步加载 同步模式--阻塞模式&#xff08;就是会阻止你浏览器的一个后续加载&#xff09;停止了后续的解析 因此停止了后续的文件加载&#xff08;图像&#xff09; 比如hifini音乐网站 二、异步加载 异步加载--xhr(重点) 比如腾讯新闻&#xff0c;腾讯招聘等 三、同…

实景三维赋能城镇数字化规划

在数字化浪潮的推动下&#xff0c;城镇规划正经历着前所未有的变革。实景三维技术以其独特的优势&#xff0c;为城镇数字化规划提供了强大的技术支持。今天&#xff0c;我们将深入探讨实景三维技术如何赋能城镇数字化规划。 一、城镇规划面临的挑战 随着城镇化进程的加快&…

Git版本控制工具--关于命令

Git版本控制工具 学习前言 在项目开发中&#xff0c;总是需要多个人同时对一个项目进行修改&#xff0c;如何高效快速地进行修改&#xff0c;且控制各自修改的版本不会和他人的进行重叠&#xff0c;这就需要用到Git分布式版本控制器了 作用 解决了一致性&#xff0c;并发性…

滑动窗口--(上篇)

滑动窗口 长度最小的子数组 给定一个含有 n 个正整数的数组和一个正整数 target 。 找出该数组中满足其总和大于等于 target 的长度最小的 子数组 [numsl, numsl1, ..., numsr-1, numsr] &#xff0c;并返回其长度**。**如果不存在符合条件的子数组&#xff0c;返回 0 。 …

flutter_鸿蒙next(win)环境搭建

第一步 拉取鸿蒙版本flutterSDK仓库 仓库地址&#xff1a;OpenHarmony-SIG/flutter_flutter 第二步 找到拉取的仓库中的README.md 并根据说明配置环境 第三步 配置好环境变量之后 用管理员开启cmd 输入&#xff1a;flutter dcotor 并查看此时flutter所支持的系统 包括&…

C++语法——多态,虚函数,纯虚函数,虚函数表,虚继承

多态&#xff1a; C实现多态的方法主要包括虚函数、纯虚函数和模板函数 其中虚函数、纯虚函数实现的多态叫动态多态&#xff0c;模板函数、重载等实现的叫静态多态。 区分静态多态和动态多态的一个方法就是看决定所调用的具体方法是在编译期还是运行时&#xff0c;运行时就叫…

arduino点亮墨水屏

前言 入学后有了自己的工位&#xff0c;越发想要用墨水屏做一个自己的工牌。于是开始在淘宝寻找墨水屏&#xff0c;发现所有的墨水屏都是排线接口&#xff0c;不会焊板子的我只能继续寻找其他方案。后来在床上一想&#xff0c;我再买个驱动板不就行了吗&#xff1f;又开始在淘…