如何在shell脚本中处理ctrl+c?

2fjabf4q  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(200)

我正在尝试在shell脚本中处理ctrl+c。我有一段在while循环中运行的代码,但是我从shell脚本中调用二进制文件,并在后台运行它,所以当我想停止二进制文件时,它应该停止。代码在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中,我添加了输入,这样我就可以将这个文件直接传递给二进制文件。

1
2


输出量:

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    
Enter the second number    
Entered number are n1 = 1 , n2 =2


而且是持续不断的。

yzuktlbb

yzuktlbb1#

更改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);
    }
}

字符串
现在,当in.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)"`
``

相关问题