diff --git a/terminawake b/terminawake old mode 100644 new mode 100755 index a746d1d..2c9467a --- a/terminawake +++ b/terminawake @@ -1,28 +1,19 @@ #!/bin/bash -# Check if the script is run as root -# EUID is an environment variable that represents the effective user ID of the current user -# The root user has an EUID of 0, so we're checking if the EUID is not 0 -# If the script is not run as root, print an error message and exit with a non-zero status code -if [[ $EUID -ne 0 ]]; then - echo "This script must be run with root privileges (sudo)." - exit 1 -fi - # Function to reenable sleep and exit the script # This function is called when the script needs to exit, to ensure that sleep is reenabled before exiting function quit { # Reenable sleep for battery and AC power modes - pmset -b disablesleep 0 - pmset -c disablesleep 0 - + sudo pmset -b disablesleep 0 + sudo pmset -c disablesleep 0 + # Exit the script exit } # Prevent sleep indefinitely for battery and AC power modes -pmset -b disablesleep 1 -pmset -c disablesleep 1 +sudo pmset -b disablesleep 1 +sudo pmset -c disablesleep 1 # Calculate the total duration in seconds from all arguments total_duration_sec=0 @@ -38,7 +29,7 @@ for arg in "$@"; do h) duration_sec=$((value * 3600)) ;; *) echo "Invalid argument: $arg"; exit 1 ;; # Exit with error message if invalid argument is provided esac - + # Add the duration in seconds to the total duration total_duration_sec=$((total_duration_sec + duration_sec)) done @@ -61,7 +52,7 @@ else sleep_pid=$! # Remove this job from the shell's active list to avoid getting a termination message disown $sleep_pid - + # Wait for user to press Enter or for the sleep duration to pass # If the user presses Enter before the duration is over, kill the sleep command if ! read -t "$total_duration_sec" -r; then