나는 읽기를 사용하여 스크립트 내에서 명령을 실행하기 위해 노력하고있어, 사용자가 Ctrl 키 + C를 사용할 때, 나는 명령의 실행을 중지하려면,하지만 종료하지 스크립트. 이 같은 뭔가 :BASH - 사용 트랩 Ctrl + C
#!/bin/bash
input=$1
while [ "$input" != finish ]
do
read -t 10 input
trap 'continue' 2
bash -c "$input"
done
unset input
사용자가 Ctrl 키 + C를 사용하여, 나는 그것이 입력을 읽고 다른 명령을 계속 실행합니다. 문제는 그 나는 같은 명령을 사용할 때 :
while (true) do echo "Hello!"; done;
내가 Ctrl 키 + C 한 시간을 입력 한 후 그것은 작동하지 않습니다,하지만 난 그것을 여러 번 입력하면 작동. 당신은 다른 프로세스 그룹의 명령을 실행해야
#!/bin/bash
# type "finish" to exit
# function called by trap
other_commands() {
printf "\rSIGINT caught "
sleep 1
printf "\rType a command >>> "
}
trap 'other_commands' SIGINT
input="[email protected]"
while true; do
printf "\rType a command >>> "
read input
[[ $input == finish ]] && break
bash -c "$input"
done
하는'트랩 시도를 '휴식'2 대신 –