Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- 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"
- echo "File download available from https://pastebin.com/2eMNadcf"
- echo
- sleep 3
- clear
- while true; do
- echo "Menu:"
- echo "1: Run a Python script"
- echo "2: Detect which version of Python you are using"
- echo "3: Make a Python file executable"
- echo "4: Exit"
- read -p "Enter your choice: " choice
- case $choice in
- 1)
- read -p "Enter the path to the Python script: " python_script
- if [ -f "$python_script" ]; then
- # Check if user has root permissions
- if [ "$EUID" -ne 0 ]; then
- echo "This operation requires root permissions. Please enter the root password:"
- sudo python3 "$python_script"
- else
- python3 "$python_script"
- fi
- sleep 3
- else
- echo "File not found."
- sleep 3
- fi
- ;;
- 2)
- echo "Available Python versions:"
- python3 -V 2>&1 | awk '{print $2}'
- python -V 2>&1 | awk '{print $2}'
- sleep 3
- ;;
- 3)
- read -p "Enter the path to the Python file: " python_file
- if [ -f "$python_file" ]; then
- # Check if user has root permissions
- if [ "$EUID" -ne 0 ]; then
- echo "This operation requires root permissions. Please enter the root password:"
- sudo chmod +x "$python_file"
- else
- chmod +x "$python_file"
- fi
- # Check if shebang is present, if not, add it
- if [[ ! $(head -n 1 "$python_file") =~ ^#!/usr/bin/env\ python[0-9\.]*$ ]]; then
- if command -v python3 &>/dev/null; then
- echo "#!/usr/bin/env python3" | cat - "$python_file" > temp && mv temp "$python_file"
- elif command -v python &>/dev/null; then
- echo "#!/usr/bin/env python" | cat - "$python_file" > temp && mv temp "$python_file"
- else
- echo "Python not found on your system."
- continue
- fi
- fi
- echo "Python file is now executable."
- else
- echo "File not found."
- fi
- sleep 3
- ;;
- 4)
- echo "Exiting the script."
- exit 0
- ;;
- *)
- echo "Invalid choice. Please select a valid option."
- sleep 3
- ;;
- esac
- done
Advertisement
Comments
-
- Bash script that detects python versions , adds shebang to py file if missing , makes the python file executable
- and runs the file MD Harrington
-
- Script updated to include test for root permissions
Add Comment
Please, Sign In to add comment
Advertisement