Skip to content

Instantly share code, notes, and snippets.

@felipealfonsog
Last active April 25, 2024 08:19
Show Gist options
  • Save felipealfonsog/6da15b5d6afab68614c0f4f095fef103 to your computer and use it in GitHub Desktop.
Save felipealfonsog/6da15b5d6afab68614c0f4f095fef103 to your computer and use it in GitHub Desktop.
This Bash script allows users to easily shutdown, suspend, or reboot their system via a simple menu interface.
#!/bin/bash
# Action Script
# Author: Felipe Alfonso González
# GitHub: github.com/felipealfonsog
# Contact: f.alfonso@res-ear.ch
# License: BSD 3-Clause License
# Function to display the options menu
display_menu() {
echo "Select an option:"
echo "1. Shutdown"
echo "2. Suspend"
echo "3. Reboot"
echo "4. Abort"
}
# Function to perform the selected action
perform_action() {
choice=$1
case $choice in
1)
sudo systemctl poweroff
;;
2)
sudo systemctl suspend
;;
3)
sudo systemctl reboot
;;
4)
echo "Operation aborted."
exit 1
;;
*)
echo "Invalid option"
;;
esac
}
# Main script starts here
echo "Are you sure you want to perform this action? (yes/no, default: yes)"
read -r confirmation
confirmation=${confirmation:-yes}
if [[ $confirmation == "yes" ]]; then
display_menu
read -p "Enter your choice: " option
perform_action "$option"
# Check if the action was successful
if [[ $? -eq 0 ]]; then
echo "Action performed successfully."
else
echo "Error occurred while performing the action."
fi
else
echo "Operation aborted."
fi
#!/bin/bash
# Action Script for macOS
# Author: Felipe Alfonso González
# GitHub: github.com/felipealfonsog
# Contact: f.alfonso@res-ear.ch
# License: BSD 3-Clause License
# Function to display the options menu
display_menu() {
echo "Select an option:"
echo "1. Shutdown"
echo "2. Suspend"
echo "3. Reboot"
echo "4. Abort"
}
# Function to perform the selected action
perform_action() {
choice=$1
case $choice in
1)
sudo shutdown -h now
;;
2)
sudo pmset sleepnow
;;
3)
sudo shutdown -r now
;;
4)
echo "Operation aborted."
exit 1
;;
*)
echo "Invalid option"
;;
esac
}
# Main script starts here
echo "Are you sure you want to perform this action? (yes/no, default: yes)"
read -r confirmation
confirmation=${confirmation:-yes}
if [[ $confirmation == "yes" ]]; then
display_menu
read -p "Enter your choice: " option
perform_action "$option"
# Check if the action was successful
if [[ $? -eq 0 ]]; then
echo "Action performed successfully."
else
echo "Error occurred while performing the action."
fi
else
echo "Operation aborted."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment