我做了一个类似以下代码的 bash
函数:
keystroke()
{
read -s -n1 -t0.1 key #Read a single keystroke for 0.1 seconds
[ "$key" = $'\e' ] && #If the pressed key is escape
{
echo Aborted by user #Display message
break #Break parent loop
}
}
每当我需要在其他bash
函数中优雅地结束loop
时,我只需调用keystroke。自从bash
v4.4.0以来,我就无法这样做了,因为它会显示:
-bash: break: only meaningful in a `for', `while', or `until' loop
如何解决这个问题,而不需要重复复制超过10次的代码?