AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1058498
Accepted
Pilot6
Pilot6
Asked: 2018-07-23 14:00:45 +0800 CST2018-07-23 14:00:45 +0800 CST 2018-07-23 14:00:45 +0800 CST

如何在 Bash 中没有 goto 处理?

  • 772

Bash 没有 goto 运算符。我有一个简单的任务,如果没有 goto 或添加新的 proc,我无法弄清楚如何解决它,这是不可取的。

我有两个条件和一个应该以这种方式工作的代码块:

[条件1]如果为真,运行一些命令并检查[条件2];如果为假,则执行一段代码。

[条件 2] 如果为真,则不执行相同的代码块;如果为假,则执行相同的块。

它是可解决的,还是我必须定义一些 proc 并exit在那里使用?我使用了一个标志变量,但我不喜欢它。

如果 goto 可用,它看起来是这样的:

[ cond 1 ] || goto label 1
  command
  ...
  command
  [ cond 2 ] && goto label 2

:label 1

block of code

:label 2

类似的东西。

command-line bash
  • 3 3 个回答
  • 30983 Views

3 个回答

  • Voted
  1. Best Answer
    Sergiy Kolodyazhnyy
    2018-07-23T14:44:04+08:002018-07-23T14:44:04+08:00

    在 shell 脚本中使用分支的典型方法是通过在主代码块之前声明的函数。但是,我认为这里的根本问题是逻辑问题,而不是 goto。显然label 1是重复的,所以这可以作为函数存在。而且,label 1为了便于阅读,条件 2 可以变成一个函数:

    #!/bin/bash
    
    label1(){
        echo "label 1 function"
    }
    
    check_cond2(){
        if ! [ condition2 ]; then
            label1 arg1 arg2
        fi
    }
    
    if [ condition1  ]; then
    
        command
        ...
        command
        check_cond2
    else 
        label1
    fi
    
    othercommand2
    othercommand3
    

    我注意到的是,在这两种情况下你都有if false exec a block of code和if false exec a block of code,所以一个想法是开始检查这些条件是否都是错误的。但是,制作类似的东西if ! [ cond1 ] || ! [ cond2 ]会改变分支逻辑。您仍然可以通过查看此帖子编辑历史来查看其伪代码版本。

    • 14
  2. Fabby
    2018-08-30T14:20:16+08:002018-08-30T14:20:16+08:00

    当我在桌面上从 Windows 迁移到 Linux 时,我有很多预先存在的.BAT文件和.CMD要转换的文件,而且我不打算为它们重写逻辑,所以我找到了一种goto在 bash 中运行的方法,因为自行goto function运行sed以去除脚本中不应运行的任何部分,然后对其全部进行评估。

    下面的源代码从原来的稍微修改,使其更健壮:

    #!/bin/bash
    
    # BAT / CMD goto function
    function goto
    {
        label=$1
        cmd=$(sed -n "/^:[[:blank:]][[:blank:]]*${label}/{:a;n;p;ba};" $0 | 
              grep -v ':$')
        eval "$cmd"
        exit
    }
    
    apt update
    
    # Just for the heck of it: how to create a variable where to jump to:
    start=${1:-"start"}
    goto "$start"
    
    : start
    goto_msg="Starting..."
    echo $goto_msg
    # Just jump to the label:
    goto "continue"
    
    : skipped
    goto_msg="This is skipped!"
    echo "$goto_msg"
    
    : continue
    goto_msg="Ended..."
    echo "$goto_msg"
    
    # following doesn't jump to apt update whereas original does
    goto update
    

    我一点也不感到内疚,正如 Linus Torvalds 的名言:

    来自:Linus Torvalds
    主题:回复:2.6.0-test* 的任何机会?
    日期:2003 年 1 月 12 日星期日 11:38:35 -0800(太平洋标准时间)

    我认为 goto 很好,而且它们通常比大量缩进更具可读性。如果代码流实际上没有自然缩进,则尤其如此(在这种情况下是这样,所以我认为使用 goto 并没有比不更清晰,但总的来说 goto 对可读性非常好)。

    当然,在像 Pascal 这样的愚蠢语言中,标签不能是描述性的,goto 可能很糟糕。但这不是 goto 的错,这是语言设计者的脑残。

    代码的原始来源 (经过修改以使其不易出错)
    引用的来源

    • 12
  3. Byte Commander
    2018-07-23T14:39:23+08:002018-07-23T14:39:23+08:00

    你应该把你block_of_code放入一个函数并使用一些 if/else:

    my_function() {
        block of code
        ...
    }
    
    if [[ condition 1 ]] ; then
        command
        ...
        command
    
        if [[ ! condition 2 ]] ; then
            # label 1
            my_function
        fi
    
    else
        # label 1
        my_function
    fi
    
    # label 2    
    
    • 11

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve