bash中是否有“goto”语句?我知道这被认为是不好的做法,但我需要特别“去”。
当前回答
该解决方案存在以下问题:
不加区别地删除所有以a结尾的代码行: 将标签:一行中的任何位置视为标签
这是一个固定的(shell-check - clean和POSIX兼容)版本:
#!/bin/sh
# GOTO for bash, based upon https://stackoverflow.com/a/31269848/5353461
goto() {
label=$1
cmd=$(sed -En "/^[[:space:]]*#[[:space:]]*$label:[[:space:]]*#/{:a;n;p;ba};" "$0")
eval "$cmd"
exit
}
start=${1:-start}
goto "$start" # GOTO start: by default
#start:# Comments can occur after labels
echo start
goto end
# skip: # Whitespace is allowed
echo this is usually skipped
# end: #
echo end
其他回答
它确实可能对一些调试或演示需求有用。
我发现Bob Copeland解决方案http://bobcopeland.com/blog/2012/10/goto-in-bash/优雅:
#!/bin/bash
# include this boilerplate
function jumpto
{
label=$1
cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
eval "$cmd"
exit
}
start=${1:-"start"}
jumpto $start
start:
# your script goes here...
x=100
jumpto foo
mid:
x=101
echo "This is not printed!"
foo:
x=${x:-10}
echo x is $x
结果:
$ ./test.sh
x is 100
$ ./test.sh foo
x is 10
$ ./test.sh mid
This is not printed!
x is 101
对于创建类似“goto”的东西,我的想法是使用带大小写的select并赋值一个变量,然后在if语句中检查该变量。不完美,但在某些情况下可能有帮助
例子:
#!/usr/bin/env bash
select goto in Ubuntu Debian Quit ; do
case $goto in
Ubuntu) { CHOICE="Ubuntu" ; break ; } ;;
Debian) { CHOICE="Debian" ; break ; } ;;
Quit) { echo "Bye" ; exit ; } ;;
*) { echo "Invalid selection, please try again..." ; } ;;
esac
done
if [ "$CHOICE" == "Ubuntu" ]; then
echo "I'm in Ubuntu"
fi
if [ "$CHOICE" == "Debian" ]; then
echo "I'm in Debian"
fi
bash中没有goto。
这里有一些肮脏的工作方法,使用陷阱只向后跳转:)
#!/bin/bash -e
trap '
echo I am
sleep 1
echo here now.
' EXIT
echo foo
goto trap 2> /dev/null
echo bar
输出:
$ ./test.sh
foo
I am
here now.
不应该以这种方式使用,而只能用于教育目的。以下是这种方法有效的原因:
Trap是使用异常处理来实现代码流中的更改。 在本例中,该陷阱将捕获导致脚本退出的任何内容。goto命令不存在,因此会抛出一个错误,该错误通常会退出脚本。此错误被trap捕获,2>/dev/null隐藏了通常显示的错误消息。
goto的这种实现显然是不可靠的,因为任何不存在的命令(或任何其他错误)都会执行相同的trap命令。特别是,您无法选择要使用哪个标签。
基本上在实际场景中,你不需要任何goto语句,它们是多余的,因为随机调用不同的地方只会让你的代码难以理解。
如果您的代码被多次调用,那么考虑使用loop并将其工作流更改为使用continue和break。
如果您的代码重复,请考虑编写函数并尽可能多次地调用它。
如果您的代码需要根据变量值跳转到特定的部分,那么可以考虑使用case语句。
如果可以将长代码分割成较小的片段,请考虑将其移动到单独的文件中,并从父脚本调用它们。
这是Hubbbitus对Judy Schmidt剧本的一个小修正。
在脚本中放置未转义的标签会导致机器崩溃。这很容易通过添加#来转义标签来解决。感谢Alexej Magura和access_granting的建议。
#!/bin/bash
# include this boilerplate
function goto {
label=$1
cmd=$(sed -n "/$#label#:/{:a;n;p;ba};" $0 | grep -v ':$')
eval "$cmd"
exit
}
start=${1:-"start"}
goto $start
#start#
echo "start"
goto bing
#boom#
echo boom
goto eof
#bang#
echo bang
goto boom
#bing#
echo bing
goto bang
#eof#
echo "the end mother-hugger..."
如果您正在测试/调试一个bash脚本,并且只想跳过一个或多个代码部分,那么这里有一种非常简单的方法,以后也很容易找到并删除(与上面描述的大多数方法不同)。
#!/bin/bash
echo "Run this"
cat >/dev/null <<GOTO_1
echo "Don't run this"
GOTO_1
echo "Also run this"
cat >/dev/null <<GOTO_2
echo "Don't run this either"
GOTO_2
echo "Yet more code I want to run"
要使脚本恢复正常,只需删除GOTO中的任何行。
我们还可以通过添加goto命令作为别名来修饰这个解决方案:
#!/bin/bash
shopt -s expand_aliases
alias goto="cat >/dev/null <<"
goto GOTO_1
echo "Don't run this"
GOTO_1
echo "Run this"
goto GOTO_2
echo "Don't run this either"
GOTO_2
echo "All done"
别名在bash脚本中通常不起作用,因此我们需要使用shopt命令来解决这个问题。
如果你想启用/禁用你的goto,我们需要更多一点:
#!/bin/bash
shopt -s expand_aliases
if [ -n "$DEBUG" ] ; then
alias goto="cat >/dev/null <<"
else
alias goto=":"
fi
goto '#GOTO_1'
echo "Don't run this"
#GOTO1
echo "Run this"
goto '#GOTO_2'
echo "Don't run this either"
#GOTO_2
echo "All done"
然后在运行脚本之前执行export DEBUG=TRUE。
标签是注释,因此如果禁用goto(通过将goto设置为':' no-op)不会导致语法错误,但这意味着我们需要在goto语句中引用它们。
无论何时使用任何类型的goto解决方案,你都需要小心,你跳过的代码不会设置你以后依赖的任何变量——你可能需要将这些定义移动到脚本的顶部,或者只是在你的goto语句之上。
推荐文章
- 如何从终端机发送电子邮件?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- Linux Bash中双&和分号有什么区别?
- 如何合并2 JSON对象从2个文件使用jq?
- 在Bash中模拟do-while循环
- 在Bash中将输出赋给变量
- 在SSH会话中查找客户端的IP地址
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- 如何在Mac OS X 10.6中使硬件发出哔哔声
- Linux删除大小为0的文件
- 从Docker容器获取环境变量
- Spring引导应用程序作为服务
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- Windows和Linux上的c++编译:ifdef开关