开发者

Are there "Goto"s in UNIX KSH scripts?

Is there a way within a KSH to exit a case statement and go to a certain line for next execution with the code? Or are there goto labels you 开发者_JS百科can use? Anything like this used instead of nesting tons of branching?


While it always helps to post a simplified example of your problem (but that covers all contingencies you expect), given your comment reply to Mark Read, you can wrap the prompting for input into a while loop, like

while ${keepTrying:-true} ; do
   echo "enter Yes or No"
   read yOrN
   case "${yOrN} in
     [Yy]* ) 
       # do something 
       keepTrying=false
     ;;
     [Nn]* )
       # do something else
        keepTrying=false
    ;;
     * )
       echo "bad input"
     ;;
   esac
done

# continue with script
# ....

Also, I agree with Mark, no labels or gotos in ksh.

I hope this helps.


There are no labels or goto. However, you can exit a deeply-nested loop by passing a number to the "break" builtin: break 2 to exit two levels, break 3 to exit three levels, etc.


There is a bash goto implementation, if you want to adapt it to ksh (I haven't so far)

#!/bin/bash

function gowto
{
    label=$1
    # works in Linux bash, but barks in ksh
    cmd=$(sed -n "/#$label:/{:a;n;p;ba};" $0 | grep -v ':$')
    #
    eval "$cmd"
    exit
}

echo step 1

gowto skip

echo To be skippped

#skip:

echo Step 2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜