0

如何为 shell 脚本定义快捷方式,当启动该脚本时,点击该快捷方式会中断该脚本或我想在该脚本中执行的操作?

4

1 回答 1

0

您可以做的一件事是循环read输入while

#!/usr/bin/env bash
runmain(){
    trap 'return' SIGINT # Stops Ctrl+C exiting, instead returns
    while((i++<=10)); do
        echo "Main function, pressing Ctrl+C will return to input menu, loop ${i}"
        sleep 3
    done
}

while true; do
    echo 'Input m to run main, or q to quit'
    read -rsn1 input
    echo "You pressed ${input}"
    case "${input}" in
        m) runmain; trap 'exit' SIGINT ;;
        q) exit ;;
        *) # default case, do nothing ;;
    esac
done

所以按 m 将运行 main 函数,按 Ctrl+C 退出,再次按 q 或 Ctrl+C 退出脚本。另一个不错的选择是使用该dialog实用程序。

查找键码的一个好方法是,在您的终端中,按 Insert 键,然后按您要查找名称的键。所以 Insert+LeftArrow 打印

^[[D

然后在case语句中,你会写

D) command ;;
于 2021-03-11T12:01:23.497 回答