在循环Linux shell中输入的提示符

时间:2022-05-27 07:58:29

I'm creating a script to check in an interactive prompt whether to delete a cron entry or not:

我正在创建一个脚本来检查是否要删除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

However the prompt part is not working, when running the script I'm getting: Please answer Y or N. Any help how to solve this ? Thank you !

然而,在运行脚本时,提示部分不工作:请回答Y或N.任何帮助如何解决这个问题?谢谢你!

2 个解决方案

#1


3  

To be able to use read in your case, you need something like this using a special file descriptor :

为了能够在您的案例中使用read,您需要使用一个特殊的文件描述符:

while read crontab_entry; do   ...
   read -u3 -p 'question' yn
   ...
done 3<&0 < crontab.out

because STDIN is already fed by the crontab output

因为STDIN已经由crontab输出提供

#2


0  

I used a special file descriptor for the cat command instead and done some modifications to the sed command to work correctly.

我为cat命令使用了一个特殊的文件描述符,并对sed命令做了一些修改,以使其正确工作。

function cron_check()
{
    crontab -l 
    result=$?
    if [[ $result -eq 0 ]]; then
        crontab -l > crontab.out
        exec 3< crontab.out
        while read -u 3 cron_entry
        do
            # ignore commented  cron entries
            [ "${cron_entry#\#}" = "$cron_entry" ] || continue # skip comment
            read -p "Do you want to remove this cron entry: $cron_entry, Please enter [Y/N]" yn
            case $yn in
                [yY] | [yY][Ee][Ss] ) cron_entry_esc=$(sed 's/[\*\.&/]/\\&/g' <<<"$cron_entry");crontab -l | sed "/$cron_entry_esc/d" | crontab -;;
                [nN] | [n|N][O|o] ) continue;;
                * ) echo "Please answer Y or N.";;
            esac
        done
    fi
}

cron_check 

#1


3  

To be able to use read in your case, you need something like this using a special file descriptor :

为了能够在您的案例中使用read,您需要使用一个特殊的文件描述符:

while read crontab_entry; do   ...
   read -u3 -p 'question' yn
   ...
done 3<&0 < crontab.out

because STDIN is already fed by the crontab output

因为STDIN已经由crontab输出提供

#2


0  

I used a special file descriptor for the cat command instead and done some modifications to the sed command to work correctly.

我为cat命令使用了一个特殊的文件描述符,并对sed命令做了一些修改,以使其正确工作。

function cron_check()
{
    crontab -l 
    result=$?
    if [[ $result -eq 0 ]]; then
        crontab -l > crontab.out
        exec 3< crontab.out
        while read -u 3 cron_entry
        do
            # ignore commented  cron entries
            [ "${cron_entry#\#}" = "$cron_entry" ] || continue # skip comment
            read -p "Do you want to remove this cron entry: $cron_entry, Please enter [Y/N]" yn
            case $yn in
                [yY] | [yY][Ee][Ss] ) cron_entry_esc=$(sed 's/[\*\.&/]/\\&/g' <<<"$cron_entry");crontab -l | sed "/$cron_entry_esc/d" | crontab -;;
                [nN] | [n|N][O|o] ) continue;;
                * ) echo "Please answer Y or N.";;
            esac
        done
    fi
}

cron_check