linux - Linux shell 循环中提示输入

标签 linux bash cron

我正在创建一个脚本来检查交互式提示是否删除 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/

相关文章:

java - Liferay - 调度程序未在给定时间触发

r - -title : invalid argument to unary operator Execution halted 错误

bash - 在curl中无限期地请求数据

linux - for 循环和 while 循环遍历目录在 bash 中的工作方式不同

php - 我需要使用 cron 作业每 30 分钟恢复一次数据库 (mysql)

mysql - 如何在救援模式下备份MySQL?

ruby-on-rails - 执行脚本并在 View 中显示输出/进度 [Ruby/Sinatra(Rails)]

linux - 我可以在不离开vim的情况下获得root权限吗?

linux - 如何过滤流/管道以删除空部分的 header ?

c - 使用 qsort 函数对结构进行排序