我已经制作了自己的信号处理程序,但我需要在之前返回
# I NEED TO JUMP HERE
echo -e "Input name of the file"
read filename
所以我可以多次输入文件名。但当我执行信号(Ctrl+C(时,我会进入处理程序,然后在执行信号的地方(所以我只能输入一次文件名(。有没有任何命令(比如C中的siglongjump和setlongjump(可以帮助我控制整个过程。
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
您可以模块化脚本,并且只在信号中打印提示处理程序:
#!/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