#!/usr/bin/env bash # Wrapper script to run Python scripts with sudo privileges # Works with both bash and zsh shells # Usage: ./run_with_sudo.sh [arguments...] # Display usage information if no arguments provided if [ $# -eq 0 ]; then echo "Usage: $0 [arguments...]" echo "This script runs the specified Python script with sudo privileges." echo "The user will be prompted for the root password." exit 1 fi # Get the Python script path (first argument) PYTHON_SCRIPT="$1" shift # Remove the first argument, leaving only the script arguments # Check if the Python script exists if [ ! -f "$PYTHON_SCRIPT" ]; then echo "Error: Python script '$PYTHON_SCRIPT' not found." exit 1 fi # Check if the Python script is executable if [ ! -x "$PYTHON_SCRIPT" ] && [[ "$PYTHON_SCRIPT" == *.py ]]; then echo "Warning: Python script '$PYTHON_SCRIPT' is not executable." echo "Consider running: chmod +x $PYTHON_SCRIPT" fi # Determine the Python interpreter to use # First try python3, then fall back to python if python3 is not available if command -v python3 &>/dev/null; then PYTHON_CMD="python3" elif command -v python &>/dev/null; then PYTHON_CMD="python" else echo "Error: Neither python3 nor python is available on this system." exit 1 fi echo "Running '$PYTHON_SCRIPT' with sudo privileges..." echo "You may be prompted for your password." # Run the Python script with sudo, passing all remaining arguments sudo "$PYTHON_CMD" "$PYTHON_SCRIPT" "$@" # Check the exit status EXIT_STATUS=$? if [ $EXIT_STATUS -ne 0 ]; then echo "The Python script exited with status code: $EXIT_STATUS" exit $EXIT_STATUS fi exit 0