ksh 中是否有类似“goto”的命令

eri*_*n00 0 ksh shell-script control-flow

这是我的脚本:

#!/bin/ksh

#this is where I want to go again if user enter 
#an answer other than "yes or no"

echo "yes or no?"
read ans

case $ans in

    [yY]*)
        echo "yes"
        ;;

    [nN]*)
        echo "no"
        ;;

    *)
        echo "yes or no only"
        # here, if the answer is not "Y" or "N", 
        # I want to go back to asking "yes or no?"
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

谁能给我一个提示?

dev*_*ull 6

你可以把read和你case在一个while循环,break出来吧,当条件满足:

while : ; do
  echo "yes or no?"
  read ans

  case $ans in
    [yY]*)
        echo "yes"
        break
        ;;
    [nN]*)
        echo "no"
        break
        ;;
    *)
        echo "yes or no only"
        ;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

while : ; do ... done代表无限循环。 break退出for, while, 或until循环。用于break在答案为yor 时退出n,否则循环将继续。