Advertisement
Mark2020H

runpythonfile.sh This makes life easier when running python scripts Menu Driven

Sep 30th, 2023 (edited)
1,144
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.78 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. echo "Bash script that detects python versions, adds shebang to py file if missing, makes the python file executable, and runs the file by MD Harrington"
  4. echo "File download available from https://pastebin.com/2eMNadcf"
  5.  
  6. echo
  7. sleep 3
  8. clear
  9.  
  10. while true; do
  11.     echo "Menu:"
  12.     echo "1: Run a Python script"
  13.     echo "2: Detect which version of Python you are using"
  14.     echo "3: Make a Python file executable"
  15.     echo "4: Exit"
  16.     read -p "Enter your choice: " choice
  17.  
  18.     case $choice in
  19.         1)
  20.             read -p "Enter the path to the Python script: " python_script
  21.             if [ -f "$python_script" ]; then
  22.                 # Check if user has root permissions
  23.                 if [ "$EUID" -ne 0 ]; then
  24.                     echo "This operation requires root permissions. Please enter the root password:"
  25.                     sudo python3 "$python_script"
  26.                 else
  27.                     python3 "$python_script"
  28.                 fi
  29.                 sleep 3
  30.             else
  31.                 echo "File not found."
  32.                 sleep 3
  33.             fi
  34.             ;;
  35.         2)
  36.             echo "Available Python versions:"
  37.             python3 -V 2>&1 | awk '{print $2}'
  38.             python -V 2>&1 | awk '{print $2}'
  39.             sleep 3
  40.             ;;
  41.         3)
  42.             read -p "Enter the path to the Python file: " python_file
  43.             if [ -f "$python_file" ]; then
  44.                 # Check if user has root permissions
  45.                 if [ "$EUID" -ne 0 ]; then
  46.                     echo "This operation requires root permissions. Please enter the root password:"
  47.                     sudo chmod +x "$python_file"
  48.                 else
  49.                     chmod +x "$python_file"
  50.                 fi
  51.                 # Check if shebang is present, if not, add it
  52.                 if [[ ! $(head -n 1 "$python_file") =~ ^#!/usr/bin/env\ python[0-9\.]*$ ]]; then
  53.                     if command -v python3 &>/dev/null; then
  54.                         echo "#!/usr/bin/env python3" | cat - "$python_file" > temp && mv temp "$python_file"
  55.                     elif command -v python &>/dev/null; then
  56.                         echo "#!/usr/bin/env python" | cat - "$python_file" > temp && mv temp "$python_file"
  57.                     else
  58.                         echo "Python not found on your system."
  59.                         continue
  60.                     fi
  61.                 fi
  62.                 echo "Python file is now executable."
  63.             else
  64.                 echo "File not found."
  65.             fi
  66.             sleep 3
  67.             ;;
  68.         4)
  69.             echo "Exiting the script."
  70.             exit 0
  71.             ;;
  72.         *)
  73.             echo "Invalid choice. Please select a valid option."
  74.             sleep 3
  75.             ;;
  76.     esac
  77. done
  78.  
Advertisement
Comments
  • Mark2020H
    1 year
    # text 0.15 KB | 0 0
    1. Bash script that detects python versions , adds shebang to py file if missing , makes the python file executable
    2. and runs the file MD Harrington
  • Mark2020H
    1 year
    # text 0.05 KB | 0 0
    1. Script updated to include test for root permissions
Add Comment
Please, Sign In to add comment
Advertisement