#!/bin/bash

# Function to check if a script is already running
is_script_running() {
    # Check if there is a process matching the script name, excluding the current process ID
    pgrep -fx "$1" | grep -v "^$$\$" >/dev/null
}

# Function to update a script
update_script() {
    local_script_name="$1"
    remote_script_url="http://www.baidu.com/$local_script_name"
    local_script_path="$(realpath "$local_script_name")"
    
    # Check if the script is running
    if is_script_running "$local_script_name"; then
        echo "Script $local_script_name is already running. Cannot update while running."
        return 1
    fi
    
    echo "Updating script $local_script_name..."
    if curl -sS "$remote_script_url" -o "$local_script_path.tmp" && \
       mv "$local_script_path.tmp" "$local_script_path"; then
        echo "Script $local_script_name updated successfully."
    else
        echo "Failed to update script $local_script_name."
    fi
}

# Display usage instructions
display_usage() {
    echo "Usage: $0 [update [<script_name>] | -h | help]"
    echo "Options:"
    echo "  update [<script_name>]: Update the specified script or all scripts if no script name is provided"
    echo "  -h, help               : Display this help message"
}

# Check if help is requested
if [[ "$1" == "-h" || "$1" == "help" ]]; then
    display_usage
    exit 0
fi

# Check if update is requested
if [ "$1" = "update" ]; then
    if [ -n "$2" ]; then
        update_script "$2"
    else
        update_script "test.sh" "test2.sh" "test3.sh"
    fi
else
    # Your script's main logic here
    echo "This is the main script."
fi
#!/bin/bash

# Function to check if the script is already running
is_script_running() {
    # Check if there is a process matching the script name, excluding the current process ID
    pgrep -fx "$0" | grep -v "^$$\$" >/dev/null
}

# Function to update the script
update_script() {
    remote_script_url="http://www.baidu.com/test.sh"
    local_script_path="$(realpath "$0")"
    
    # Check if the script is running
    if is_script_running; then
        echo "Script is already running. Cannot update while running."
        exit 1
    fi
    
    echo "Updating script..."
    if curl -sS "$remote_script_url" -o "$local_script_path.tmp" && \
       mv "$local_script_path.tmp" "$local_script_path"; then
        echo "Script updated successfully."
    else
        echo "Failed to update script."
    fi
}

# Check if update is requested
if [ "$1" = "update" ]; then
    update_script
else
    # Your script's main logic here
    echo "This is the main script."
fi