bashrc function to auto installs missing linux programs and missing python modules!
Page 1 of 1
bashrc function to auto installs missing linux programs and missing python modules!
bashrc function to auto installs missing linux programs and missing python modules! (even auto re runs the script that caused the auto install)!
to edit your bashrc file
to edit your bashrc file
- 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
# Function to handle missing Python modules and install them
pip_command_not_found_handle() {
local cmd=$1
echo "Attempting to install missing module '$cmd'..."
# Activate the virtual environment if it exists
if [ -d "venv" ]; then
source venv/bin/activate
echo "Using virtual environment: venv"
else
python -m venv myenv
source myenv/bin/activate
fi
# Attempt to install the missing package
if pip install "$cmd"; then
echo "Module '$cmd' installed successfully."
else
echo "Failed to install '$cmd'. Please install it manually."
return 1
fi
}
# Override the python command to automatically handle missing modules
python() {
output=$(command python "$@" 2>&1) # Capture the output and error of the Python command
# Check for "ModuleNotFoundError" in the output
if echo "$output" | grep -q "ModuleNotFoundError"; then
local missing_module=$(echo "$output" | grep -oP "No module named '\K[^']+")
if [ -n "$missing_module" ]; then
echo "Module '$missing_module' not found! Attempting to install it..."
pip_command_not_found_handle "$missing_module"
# Retry the script after attempting to install the missing module
echo "Retrying to run the Python command..."
output=$(command python "$@") # Run the command again
fi
fi
# Output the result or error
echo "$output"
}
# Ensure the function works with a script argument as well
alias runpy="python"
Similar topics
» Auto Install Missing Programs on linux using bashrc
» using figlet in bashrc in linux
» Linux Extract Archive Bashrc Function
» Bashrc function to take screenshot on linux for raspberry pi
» Video eding and cutting video in linux using free easy installs
» using figlet in bashrc in linux
» Linux Extract Archive Bashrc Function
» Bashrc function to take screenshot on linux for raspberry pi
» Video eding and cutting video in linux using free easy installs
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum