UNIX KSH 脚本中是否有“Goto”?

发布于 2024-11-27 00:31:25 字数 77 浏览 1 评论 0原文

KSH 中有没有办法退出 case 语句并转到某一行以便下次执行代码?或者有可以使用的 goto 标签吗?使用类似的东西而不是嵌套大量分支?

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 can use? Anything like this used instead of nesting tons of branching?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是我以为 2024-12-04 00:31:25

虽然发布问题的简化示例总是有帮助的(但这涵盖了您期望的所有意外情况),但考虑到您对 Mark Read 的评论回复,您可以将输入提示包装到 while 循环中,例如

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
# ....

另外,我同意 Mark, ksh 中没有标签或 goto。

我希望这有帮助。

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.

说谎友 2024-12-04 00:31:25

没有标签或跳转。但是,您可以通过将数字传递给“break”内置函数来退出深度嵌套循环:break 2 退出两级,break 3 退出三级,等等。

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.

夜清冷一曲。 2024-12-04 00:31:25

有一个 bash goto 实现,如果你想使其适应 ksh (我到目前为止还没有)

#!/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

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
意中人 2024-12-04 00:31:25

我创建了名为 GOTO.ksh 的以下脚本:

#!/bin/ksh
#set -xv

OLD="$0"
NEW=/tmp/_$(basename "$0")
rm -f "$NEW"

grep "^#GOTO " "$OLD" | while read A B C
do
    [ -f "$NEW" ] || cp "$OLD" "$NEW"
    # printf "A=$A\nB=$B\nC=$C\n\n"
    sed -e "/^#GOTO $B/,/^#$B/s@^@\#. @" < "$NEW" > "$NEW"x && mv "$NEW"x "$NEW"
done
[ -f "$NEW" ] && { echo rm \"$NEW\" >> "$NEW" ;sync ; exec ksh "$NEW" $@; }

在 ksh 脚本中,需要 goto(用于调试),我按如下方式调用它:

. ~/bin/GOTO.ksh

现在,如果确实想跳过 ksh 命令,只需输入:

#GOTO labelA
.... commands to be skipped ....
#labelA

.... commands to be executed ....

#GOTO labelB
.... commands to be skipped ...
#labelB

如果脚本中,行将被注释掉,脚本将被写入 /tmp(前面带有“_”)并从那里执行。

I create the follwing script called GOTO.ksh:

#!/bin/ksh
#set -xv

OLD="$0"
NEW=/tmp/_$(basename "$0")
rm -f "$NEW"

grep "^#GOTO " "$OLD" | while read A B C
do
    [ -f "$NEW" ] || cp "$OLD" "$NEW"
    # printf "A=$A\nB=$B\nC=$C\n\n"
    sed -e "/^#GOTO $B/,/^#$B/s@^@\#. @" < "$NEW" > "$NEW"x && mv "$NEW"x "$NEW"
done
[ -f "$NEW" ] && { echo rm \"$NEW\" >> "$NEW" ;sync ; exec ksh "$NEW" $@; }

In the ksh script were the goto's are needed (for debugging) I invoke this as follows:

. ~/bin/GOTO.ksh

Now if one does want to skip ksh commands just put in:

#GOTO labelA
.... commands to be skipped ....
#labelA

.... commands to be executed ....

#GOTO labelB
.... commands to be skipped ...
#labelB

If there are #GOTO lines in the script, lines will be commented out, and the script will be written to /tmp (prepended with "_") and executed from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文