我正在创建一个脚本来检查交互式提示是否删除 cron 条目:
function cron_check()
{
crontab -l
result=$?
if [[ $result -eq 0 ]]; then
crontab -l > crontab.out
cat crontab.out | while read cron_entry
do
# ignore commented cron entries
[ "${cron_entry#\#}" = "$cron_entry" ] || continue # skip comment
read -u3 -p "Do you want to remove this cron entry: $cron_entry, Please enter [Y/N]" yn
case $yn in
[yY] | [yY][Ee][Ss] ) sed -i "/$cron_entry/d" crontab.out;;
[nN] | [n|N][O|o] ) break;;
* ) echo "Please answer Y or N.";;
esac
done 3<&0 < crontab.out
crontab crontab.out
fi
}
cron_check
但是,提示部分不起作用,当运行脚本时,我得到:请回答 Y 或 N。如何解决此问题有任何帮助吗? 谢谢!
最佳答案
为了能够在您的情况下使用 read,您需要使用特殊的文件描述符:
while read crontab_entry; do ...
read -u3 -p 'question' yn
...
done 3<&0 < crontab.out
因为 STDIN 已经由 crontab 输出提供
关于linux - Linux shell 循环中提示输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27676438/