退出bash脚本并转向“endex”

时间:2014-05-17 11:11:26

标签: bash

我在

中有这一行的脚本
read -t 1800 -n1 -p ""$bldgrn" Do you want to update the mpd feed? [y/n/q/r] $(tput sgr0)" ynqr

在30分钟不活动后退出脚本。我怎样才能转向一个名为“endex”的函数?

“endex”是

function endex {
    echo;
    sleep 0;
    clear;
    exit
}

1 个答案:

答案 0 :(得分:3)

如果read超时,则其返回代码大于128。所以

read -t 1800 -n 1 -p "${bldgrn}Do you want to update the mpd feed? [y/n/q/r]$(tput sgr0) " ynqr
if (($?>128)); then
    endex
else
    # no timeout here
fi

会做的。

help read-t选项的说明中包含该信息。


确保{/ 1}}在读取语句之后。如果您需要一些中间命令,请保存if (($?>128))的返回码:

read