How can we get back in the right place (that we specify) after signal?

35 Views Asked by At

I have made my own signal handler, but I need to get back before

# I NEED TO JUMP HERE

echo -e "Input name of the file"
read filename

so I could input filename several times. But when I execute signal (Ctrl + C), I go into handler and then in the place where I execute signal (so I can input filename only once). Is there any command (like siglongjump and setlongjump in C), that help me control the whole process.

count=0
flag=0

handl(){
echo
if test $flag -eq 0
then echo "You did not input filename"
fi

file $filename | grep "C source" > /dev/null
a=$?

if test $a -eq 0
then
count=$((count+1))
fi
echo "Number of C source files: $count"
}

trap handl 2

echo -e "Input name of the file"
read filename
flag=1

sleep 1
1

There are 1 best solutions below

0
On BEST ANSWER

You can modularize your script and only print a prompt in the signal handler:

#!/usr/bin/env bash

count=0
flag=0

handl(){
    echo
    if test $flag -eq 0
    then echo "You did not input filename"
         prompt
         return
    fi

    file "$filename" | grep "C source" > /dev/null
    a=$?

    if test $a -eq 0
    then
        count=$((count+1))
    fi
    echo "Number of C source files: $count"
}

prompt(){
    echo -e "Input name of the file"
}

input(){
    read -r filename
    flag=1
    echo sleep 1
    sleep 1
}

trap handl 2

while true
do
    prompt
    input
done