-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep_timer_macos.zsh
49 lines (42 loc) · 1.33 KB
/
sleep_timer_macos.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/zsh
# Function to validate if the input is a positive integer
function is_positive_integer() {
[[ "$1" =~ ^[1-9][0-9]*$ ]]
}
# Ask the user for the action they want to perform and validate the input
while true; do
read "action?Would you like to send this device to sleep (s) or shut it down (d)? "
case "$action" in
[sS]) action="sleep"; break;;
[dD]) action="shutdown"; break;;
*) echo "Invalid choice. Please enter 's' for sleep or 'd' for shutdown.";;
esac
done
# Ask the user for the duration and validate the input
while true; do
read "duration?In how many minutes would you like to $action this device? "
if is_positive_integer "$duration"; then
break
else
echo "Invalid input. Please enter a positive number."
fi
done
# Calculate the sleep/shutdown time
end_time=$(date -v +${duration}M +"%H:%M:%S")
echo "This device will $action at $end_time."
# Function to handle the abort operation
trap 'echo -e "\nOperation aborted."; exit 1' SIGINT
# Countdown loop
while (( duration > 0 )); do
sleep 60
(( duration-- ))
if (( duration > 0 )); then
echo "$duration minutes left until $action."
fi
done
# Perform the selected action
if [[ "$action" == "sleep" ]]; then
pmset sleepnow
elif [[ "$action" == "shutdown" ]]; then
sudo shutdown -h now
fi