Scheduled Shutdown

Introduction

This is a bash script code that helps the user turn off their computer automatically using a timer previously entered by the user.

How it works

These are the steps that i took when i was writing the code:

Step #1: Adding the Username

#!/bin/bash
user_name=$(whoami)
echo "Hey, $user_name!"
  • the “#!/bin/bash” tells the system that the code is a bash script, it is also known as the shebang.

  • “whoami” function checks for the current user.

  • The dollar sign function “$“ helps to retrieve whatever information is inside the brackets, therefore

    $(whoami) retrieves whatever whoami is and stores it

  • user_name=$(whoami) helps to check, retrieve and save whatever the output is in the user_name variable

Step #2: Validating the time till shutdown

echo "When would you like to turn this PC off (HH:MM)" 
is_valid_time() { 
[[ "$1" =~ ^([01]?[0-9]|2[0-3]):[0-5][0-9]$ ]] 
}
  • echo prints whatever is written

  • is_valid_time() function makes the user put in the correct time format, in this case the (HH:MM) hour and minute format

is_valid_time() { [[ "$1" =~ ^([01]?[0-9]|2[0-3]):[0-5][0-9]$ ]] } : is a regular expression REGEX pattern let me simplify it

  • "$1" is the first positional argument

  • ^ is the start of the string of the pattern

  • [01]?[0-9] approves of any number between 0-9, 00-09, 10-19

  • |: stand for OR

  • 2[0-3] approves a number beginning with 2 and ending 0,1,2 or 3

  • [0-5][0-9] approves of any number from 00-59

  • $ marks the end of the string

Step #3: Using the IF and ELSE function to run a loop

while true; do
    read -p "Enter shutdown time (HH:MM format, 24-hour): " shutdown_time

    if is_valid_time "$shutdown_time"; then
        echo "You entered: $shutdown_time"
        break
    else
        echo "❌ Invalid format...."
    fi
done
echo "This PC will shut down at $shutdown_time!"
  • while true; do fuction checks whether any value has been inputed, if not it keeps the user in a loop, if yes it moves them to the if and else statement

  • read function helps to record whatever value is inputted by the user and saves it to the variable shutdown_time

  • -p function helps to add a prompt to the read function to make it interactive for the user, it is also used a substitute for the echo function

  • if is_valid_time "$shutdown_time"; then checks whether the value inputted is correct and moves them to the next stage

  • echo "You entered: $shutdown_time" prints the value in ““

  • break helps to stop the loop once the correct value has been inputted

  • else echo "❌ Invalid format...." prints invalid format if the wrong value is inputted

  • fi done helps to break the loop once the user is done

  • echo "This PC will shut down at $shutdown_time!"

Step #4: Scheduled shutdown

user_name=$(whoami)
echo "Goodbye, $user_name!"
sudo shutdown -h $shutdown_time
  • $user_name! inputs the value stored for username

  • sudo uses admin priviledges so it asks for the password before it shuts down

  • -h halts the whole process till the scheduled time for shutdown

Final Result


#!/bin/bash
user_name=$(whoami)
echo "Hey, $user_name!"
read -p "Kindly input your E-mail, You'll be notified of your Pc's shutdown before hand: " E-mail
Email= $(E-mail)
echo "When would you like to turn this PC off (HH:MM)" 
is_valid_time() { 
[[ "$1" =~ ^([01]?[0-9]|2[0-3]):[0-5][0-9]$ ]] 
}
while true; do
    read -p "Enter shutdown time (HH:MM format, 24-hour): " shutdown_time

    if is_valid_time "$shutdown_time"; then
        echo "You entered: $shutdown_time"
        break
    else
        echo "❌ Invalid format...."
    fi
done
echo "This PC will shut down at $shutdown_time!"
user_name=$(whoami)
echo "Goodbye, $user_name!"
sudo shutdown -h $shutdown_time

0
Subscribe to my newsletter

Read articles from Israel Folorunsho directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Israel Folorunsho
Israel Folorunsho