在bash向前声明? [英] Forward declarations in bash?

查看:196
本文介绍了在bash向前声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有庆典这样的事情,或者至少像前向声明类似的东西(解决方法)(在 C / C ++ ,例如)?

还是有这么回事,因为它是在一个通始终执行?

如果没有前向声明,我应该怎么做才能让我的剧本更容易阅读(这是相当长,在开始这些函数定义,全局变量混合,使我的剧本看起来非常难看又难读/理解)?我要求对这种情况下一些知名的/最佳实践。


例如:

 #像向前声明
函数func函数的执行#
FUNCFUNC的定义#
函数func
{
    123回声
}


解决方案

大问题。我用这样的方式为大多数我的脚本:

 #!/斌/庆典主(){
    富
    酒吧
    巴兹
}富(){
}栏(){
}巴兹(){
}主要的$ @

可以读从上到下的code,但它并没有真正开始执行,直到最后一行。通过传递$ @来的main(),您可以访问命令行参数 $ 1 $ 2 等就像你通常会。

Is there such thing in bash or at least something similar (work-around) like forward declarations (well known in C/C++, for example)?

Or there's so such thing, because it's always executed in one pass?

If there are no forward declarations, what should I do to make my script easier for reading (it's rather long and these function definitions at the beginning, mixed with global variables, make my script looks very, very ugly and hard for reading/understanding)? I'm asking for some well-known/best practices for such cases.


For example:

# something like forward declaration
function func

# execution of the function
func

# definition of func
function func
{
    echo 123
}

解决方案

Great question. I use a pattern like this for most of my scripts:

#!/bin/bash

main() {
    foo
    bar
    baz
}

foo() {
}

bar() {
}

baz() {
}

main "$@"

You can read the code from top to bottom, but it doesn't actually start executing until the last line. By passing "$@" to main() you can access the command-line arguments $1, $2, et al just as you normally would.

这篇关于在bash向前声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆