스크립트에서 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로 파이핑하는지 잘 모르겠지만 주석 처리된 트랩 명령은 fwiw mask대신 사용하지 않는 한 계속 작동할 수 있습니다 unmask. for를 캡처하고 +를 누르면 명령이 두 번 실행된다는 점은 주목할 INT가치 가 EXIT있습니다 .ctrlc

답변2

나는 시도했다jesse_b의 답변Ctrl위의 내용 은 +가 C프로세스를 일종의 차단하기 때문에 나에게 작동하지 않았기 때문에 내가 해야 할 일은 트랩 호출 앞에 함수를 넣는 것뿐이었습니다.

#!/bin/bash

trp()
{
    sudo airmon-ng stop wlp2s0mon
    service NetworkManager start
}

trap trp SIGINT

sudo airmon-ng check kill
sudo airmon-ng start wlp2s0
sudo aireplay-ng --deauth 0 -a bb:bb:bb:bb:bb:bb -c cc:cc:cc:cc:cc:cc wlp2s0mon

따라서 여기서는 트랩이 성공적으로 사용되기 전에 트랩 이전에 함수를 등록합니다. 그렇지 않으면 함수가 이 aireplay-ng줄 뒤에 있으면 다음 오류가 발생합니다.

Line 1: trp: command not found

궁금하신 분들을 위해 말씀드리자면, 이것은 제 조카가 TikTok을 떠나 잠자리에 들 수 있도록 자정에 Wi-Fi를 끄게 만드는 스크립트일 뿐입니다.

관련 정보