PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Auto Install Missing Programs on linux using bashrc

Go down

Auto Install Missing Programs on linux using bashrc Empty Auto Install Missing Programs on linux using bashrc

Post by jamied_uk 7th August 2024, 12:52

Code:
sudo gedit ~/.bashrc




Code:

# Function to handle missing commands and install them
command_not_found_handle() {
    local cmd=$1
    # Check if the command is available
    if ! command -v "$cmd" &> /dev/null; then
        echo "Command '$cmd' not found. Installing..."
        sudo apt-get update -y
        sudo apt-get install -y "$cmd"
        # Check if the installation was successful
        if command -v "$cmd" &> /dev/null; then
            echo "Command '$cmd' installed successfully."
        else
            echo "Failed to install '$cmd'."
        fi
    else
        # If the command exists, handle it normally
        "$@"
    fi
}

# Override the default command_not_found_handle function
if [ -n "$BASH_VERSION" ]; then
    command_not_found_handle() {
        local cmd=$1
        if ! command -v "$cmd" &> /dev/null; then
            echo "Command '$cmd' not found. Installing..."
            sudo apt-get update -y
            sudo apt-get install -y "$cmd"
            if command -v "$cmd" &> /dev/null; then
                echo "Command '$cmd' installed successfully."
            else
                echo "Failed to install '$cmd'."
            fi
        else
            "$@"
        fi
    }
fi
jamied_uk
jamied_uk
Admin

Posts : 3053
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum