AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1431631
Accepted
fixit7
fixit7
Asked: 2022-09-22 14:47:22 +0800 CST2022-09-22 14:47:22 +0800 CST 2022-09-22 14:47:22 +0800 CST

在脚本中捕获 Ctrl+C

  • 772

我有这个正是我想要的方式。

但是我可以弄清楚如何捕获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
bash
  • 1 1 个回答
  • 117 Views

1 个回答

  • Voted
  1. Best Answer
    Terrance
    2022-09-23T16:44:42+08:002022-09-23T16:44:42+08:00

    以下是如何CTRL+C在脚本中捕获 。您不会调用该ctrl_c函数,因为当您按键盘上的CTRL+时会调用该函数。C我添加了一个exit 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
    
    • 2

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve