スクリプトからCtrl Cをキャプチャする

スクリプトからCtrl Cをキャプチャする

スクリプトを早く終了するには、一時停止と休止状態を再度有効にするために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

ベストアンサー1

スクリプトの上部付近に以下を追加します。

trap cleanup SIGINT

cleanup () {
    sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
}

marlin再度有効にしたときにsystemctlにパイピングするのはわかりませんが、注釈付きトラップコマンドはfwiwmaskの代わりに使用しない限り機能し続けることができますunmask。 forをキャプチャして+を押すと、コマンドが2回実行されることに注意するINT価値がEXITあります。ctrlc

おすすめ記事