我得到的正是我想要的。
但我可以弄清楚如何捕获Ctrl+ C,这样如果我想提前结束脚本,它可以重新启用挂起和休眠模式。
我查看了有关捕获Ctrl+的其他讨论C,但发现没有任何帮助。
谢谢。
# TimerInTerminal.sh
# To prevent your Linux system from suspending or going into hibernation, you need to disable the following systemd targets:
# sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# To re-enable the suspend and hibernation modes, run the command:
# sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
soundfile="/usr/share/sounds/My_Sounds/Electronic_Chime.wav"
# Stop computer from sleeping while timer is running
# prevent your Linux system from suspending or going into hibernation
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# This allows supend ?
#trap "echo marlin | sudo -S systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target" INT EXIT
if [ $# -eq 1 ]
then
DURATION="$1"
else
read -r -p "Timer for how many minutes?( for fractional, use decimal notation , 0.5==30s, 1.25==75s etc) : " DURATION
read -r -p "Enter text to display at the end of the timer : " n1
fi
DURATION=$(echo "$DURATION * 60 / 1" | bc) # lets us deal with fractional inputs
START=$(date +%s) # only do this once (anchor's the time)
countdown () {
NOW=$(date +%s) # Get time now in seconds
DIF=$((NOW - START)) # Compute diff in seconds
ELAPSE=$((DURATION - DIF)) # Compute elapsed time in seconds
MINS=$((ELAPSE / 60)) # Convert to minutes... (dumps remainder from division)
SECS=$((ELAPSE - (MINS*60))) # ... and seconds
#banner "$MINS:$SECS"
echo "$MINS:$SECS"
sleep "$1"
}
while true
do
clear
countdown 0 # calc time remaining
if [ $MINS -le 0 ]
then
# Blink screen
while [ $SECS -gt 0 ]
do
clear # Flash on
#setterm -term linux -back red -fore white
countdown 0.5
clear # Flash off
#setterm -term linux -default
countdown 0.5
done # End for loop
setterm -term linux -default
clear
break # time has expired lets get out of here
else
countdown 1
fi
done
echo $n1
amixer -D pulse sset Master 30% > /dev/null 2>&1
# Play a sound
cvlc --play-and-exit "$soundfile" > /dev/null 2>&1
# To re-enable the suspend and hibernation modes, run the command:
echo marlin | sudo -S systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
这有效。我不会将整个代码插入 ctrl_c 中,然后看看会发生什么。
#!/bin/bash
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
echo "** Trapped CTRL-C"
echo System will now have suspension/hibernation halted.
# prevent your Linux system from suspending or going into hibernation
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
sleep 10
}
ctrl_c
答案1
以下是如何在脚本中捕获CTRL+C
。您无需调用该函数,因为按下键盘上的+键ctrl_c
时会调用该函数。我添加了一个,以便在捕获 后退出脚本并停止循环。CTRLCexit 0
CTRL+C
for .. loop
cat ctrl_test.bsh
#!/bin/bash
#Setup trap command, assign what to call and what is the trigger (SIGINT)
trap ctrl_c INT
#Function of what trap command calls
function ctrl_c() {
printf "\n** Trapped CTRL-C after $i seconds.\n"
exit 0
}
#Rest of script to print counting numbers to screen will not break with CTRL+C due to trap
for ((i=1;i>0;i++))
do
printf "\r$i"
sleep 1
done
例子:
terrance@terrance-ubuntu:~$ ./ctrl_test.bsh
13^C
** Trapped CTRL-C after 13 seconds.
terrance@terrance-ubuntu:~$
在您的脚本中,您可以执行如下操作:
#TimerInTerminal.sh
#Trap Ctrl+C and re-enable suspend and hibernation modes
trap ctrl_c INT
function ctrl_c() {
sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
exit 0
}
# To prevent your Linux system from suspending or going into hibernation, you need to disable the following systemd targets:
# sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# To re-enable the suspend and hibernation modes, run the command:
# sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
soundfile="/usr/share/sounds/My_Sounds/Electronic_Chime.wav"
# Stop computer from sleeping while timer is running
# prevent your Linux system from suspending or going into hibernation
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
# This allows supend ?
#trap "echo marlin | sudo -S systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target" INT EXIT
if [ $# -eq 1 ]
then
DURATION="$1"
else
read -r -p "Timer for how many minutes?( for fractional, use decimal notation , 0.5==30s, 1.25==75s etc) : " DURATION
read -r -p "Enter text to display at the end of the timer : " n1
fi
DURATION=$(echo "$DURATION * 60 / 1" | bc) # lets us deal with fractional inputs
START=$(date +%s) # only do this once (anchor's the time)
countdown () {
NOW=$(date +%s) # Get time now in seconds
DIF=$((NOW - START)) # Compute diff in seconds
ELAPSE=$((DURATION - DIF)) # Compute elapsed time in seconds
MINS=$((ELAPSE / 60)) # Convert to minutes... (dumps remainder from division)
SECS=$((ELAPSE - (MINS*60))) # ... and seconds
#banner "$MINS:$SECS"
echo "$MINS:$SECS"
sleep "$1"
}
while true
do
clear
countdown 0 # calc time remaining
if [ $MINS -le 0 ]
then
# Blink screen
while [ $SECS -gt 0 ]
do
clear # Flash on
#setterm -term linux -back red -fore white
countdown 0.5
clear # Flash off
#setterm -term linux -default
countdown 0.5
done # End for loop
setterm -term linux -default
clear
break # time has expired lets get out of here
else
countdown 1
fi
done
echo $n1
amixer -D pulse sset Master 30% > /dev/null 2>&1
# Play a sound
cvlc --play-and-exit "$soundfile" > /dev/null 2>&1
# To re-enable the suspend and hibernation modes, run the command:
echo marlin | sudo -S systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target