bash - 如何在 shell 脚本中处理 Ctrl + c?
问题描述
我正在尝试处理 shell 脚本中的 ctrl + c 。我有代码在while循环中运行,但我从脚本调用二进制文件并在后台运行它,所以当我想停止二进制文件时应该停止。代码如下 hello.c
vim hello.c
#include <stdio.h>
int main()
{
while(1)
{
int n1,n2;
printf("Enter the first number\n");
scanf("%d",&n1);
printf("Enter the second number\n");
scanf("%d",&n2);
printf("Entered number are n1 = %d , n2 =%d\n",n1,n2);
}
}
下面是我使用的 Bash 脚本。
#/i/bin/sh
echo run the hello binary
./hello < in.txt &
trap_ctrlc()
{
ps -eaf | grep hello | grep -v grep | awk '{print $2}' | xargs kill -9
echo trap_ctrlc
exit
}
trap trap_ctrlc SIGHUP SIGINT SIGTERM
启动脚本后,hello 二进制文件将连续运行。我已经使用 kill -9 pid 命令从其他终端杀死了这个二进制文件。
我已经尝试过这个 trap_ctrlc 功能,但它不起作用。如何在 shell 脚本中处理 Ctrl + c。在 in.txt 中,我添加了输入,因此我可以将此文件直接传递给二进制
vim in.txt
1
2
输出:
输入第一个数字
输入第二个数字
输入的数字是 n1 = 1 , n2 =2
输入第一个数字
输入第二个数字
输入的数字是 n1 = 1 , n2 =2
输入第一个数字
输入第二个数字
输入的数字是 n1 = 1 , n2 =2
并且它一直在进行。
解决方案
更改您的c程序,以便检查读取数据是否实际成功:
#include <stdio.h>
int main()
{
int n1,n2;
while(1) {
printf("Enter the first number\n");
if(scanf("%d",&n1) != 1) return 0; /* check here */
printf("Enter the second number\n");
if(scanf("%d",&n2) != 1) return 0; /* check here */
printf("Entered number are n1 = %d , n2 =%d\n",n1,n2);
}
}
它现在将在输入 fromin.txt
耗尽时终止。
in.txt
要制作多次读取的内容,您可以在bash脚本中创建一个循环,该循环将./hello
永远提供(或直到它被杀死)。
例子:
#!/bin/bash
# a function to repeatedly print the content in "in.txt"
function print_forever() {
while [ 1 ];
do
cat "$1"
sleep 1
done
}
echo run the hello binary
print_forever in.txt | ./hello &
pid=$!
echo "background process $pid started"
trap_ctrlc() {
kill $pid
echo -e "\nkill=$? (0 = success)\n"
wait $pid
echo "wait=$? (the exit status from the background process)"
echo -e "\n\ntrap_ctrlc\n\n"
}
trap trap_ctrlc INT
# wait for all background processes to terminate
wait
可能的输出:
$ ./hello.sh
run the hello binary
background process 262717 started
Enter the first number
Enter the second number
Entered number are n1 = 1 , n2 =2
Enter the first number
Enter the second number
Entered number are n1 = 1 , n2 =2
Enter the first number
^C
kill=0 (0 = success)
wait=143 (the exit status from the background process)
trap_ctrlc
另一种选择是在wait
中断后杀死孩子:
#!/bin/bash
function print_forever() {
while [ 1 ];
do
cat "$1"
sleep 1
done
}
echo run the hello binary
print_forever in.txt | ./hello &
pid=$!
echo "background process $pid started"
trap_ctrlc() {
echo -e "\n\ntrap_ctrlc\n\n"
}
trap trap_ctrlc INT
# wait for all background processes to terminate
wait
echo first wait=$?
kill $pid
echo -e "\nkill=$? (0 = success)\n"
wait $pid
echo "wait=$? (the exit status from the background process)"`
``
推荐阅读
- python - 如何从列表中删除所有非字母?
- magento - 如何应用 magento 1 MPERF-10509 补丁
- raspberry-pi3 - Raspberry Pi 3B+ 上 scons 和 ws281x 的 Yocto 配方问题
- python - 为什么 Beautiful Soup 只提取 CDATA 而不是常规评论?
- mysql - SQL如何获得33%最便宜产品的最高价格
- scipy - Scipy 优化约束
- elasticsearch - 如何在 logstash 中使用脚本 upsert 来更新文档
- animation - 构建后不会触发 CSSTransitions。(盖茨比)
- python - Pygame Snake 游戏中的 Snake 长度
- object - Treeview 中的 return-object 导致“超出最大调用堆栈大小”Vuetify