我将如何循环这个命令?

我将如何循环这个命令?

我的代码片段如下所示。如果脚本无法找到路径,则当前将关闭。

[echo "could not find $REPLY, ensure the path is correct and try again"] 

相反,我希望它在此时循环并尝试再次接受路径的输入。我该怎么做?

while [ $choice -eq 3 ]; do

read choice
if [ $choice -eq 1 ] ; then

    echo "You have chosen to spec....  '/path/../'" 
    read
    if [ -d $REPLY ]; then
        find $REPLY -type f -printf '%TY-%Tm-%Td %.8TT %p\n '| sort -r | head -20
    else 
        echo "could not find $REPLY, ensure the path is correct and try again"
    fi
else

答案1

while true; do
    echo "You have chosen to spec....  '/path/../'" 
    read
    if [ -d "$REPLY" ]; then
        find "$REPLY" -type f -printf '%TY-%Tm-%Td %.8TT %p\n '| sort -r | head -20
        break
    else
        echo "could not find $REPLY, ensure the path is correct and try again"
        echo "press enter to continue..."
        read cont
    fi
done

答案2

我可能会用while case:

i=0 c=3
while case "$((i+=!(c==1))).$c"   in 
      ([MAX_REPEAT_NUM].*) ! break;;
      (*.3) read c; continue      ;;
      (*.1) printf 'prompt> '
            read d || continue    ;;esac
do    [ -d "$d" ] || continue
      find "$REPLY" -type f \
            -printf '%TY-%Tm-%Td %.8TT %p\n '| 
      sort -r | head -20
done

您可以通过这种方式测试可能性分支。

相关内容