Bash - 在第一次调用后在脚本中编写函数定义(作为 GOTO/跳转问题)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8249040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash - writing function definition in script after first call (as a GOTO/jump problematics)
提问by sdaau
I basically want to write me a bash script, where I'd generate a couple of big files using heredoc; and then run some commands using those files.
我基本上想给我写一个 bash 脚本,在那里我会使用heredoc生成几个大文件;然后使用这些文件运行一些命令。
It is understood that (obviously) the heredoc files need to be generated before the commands run - however, what irritates me in that arrangement, is that I must also write the 'heredoc' statements code, beforeI write the command code.
据了解,(显然)需要在命令运行之前生成 heredoc 文件 - 然而,在这种安排中让我恼火的是,在我编写命令代码之前,我还必须编写“heredoc”语句代码。
So I thought I'd write the heredoc statements in a function - but still the same problem here: Chapter 24. Functionssays:
所以我想我会在一个函数中编写 heredoc 语句 - 但这里仍然存在同样的问题:第 24 章函数说:
The function definition must precede the first call to it. There is no method of "declaring" the function, as, for example, in C.
函数定义必须在第一次调用之前。没有“声明”函数的方法,例如在 C 中。
Indeed, it is so:
确实是这样:
$ cat > test.sh <<EOF
testo
function testo {
echo "a"
}
EOF
$ bash test.sh
test.sh: line 1: testo: command not found
Then I thought maybe I could place some labels and jump around with GOTO, as in (pseudocode):
然后我想也许我可以放置一些标签并使用 跳转GOTO,如(伪代码):
$ cat > test.sh <<EOF
goto :FUNCLABEL
:MAIN
testo
goto :EXIT
:FUNCLABEL
function testo {
echo "a"
}
goto MAIN
:EXIT
... but it turns out BASH gotodoesn't exist either.
...但事实证明BASH goto也不存在。
My only goal is that - I want to first write the "core" of the script file, which is some five-six commands; and only thenwrite the heredoc statements in the script file (which may have hundreds of lines); having the heredocs first really makes reading the code difficult for me. Is there any way to achieve that?
我唯一的目标是——我想先写脚本文件的“核心”,也就是一些五六个命令;只有然后写在脚本文件中的定界符语句(可能有数百行); 首先拥有heredocs确实让我很难阅读代码。有什么方法可以实现吗?
回答by William Pursell
A common technique is:
一种常见的技术是:
#!/bin/sh
main() {
cmd_list
}
cat > file1 << EOF
big HEREDOC
EOF
main
回答by jaypal singh
In Bash you will have to define the entire function before calling it. If you want to write the core script first then you can write the heredoc statements in another script file and call it whenever you feel like and assign the values returned (may be) to your core script.
在 Bash 中,您必须在调用之前定义整个函数。如果您想先编写核心脚本,那么您可以在另一个脚本文件中编写 heredoc 语句,并在需要时调用它并将返回的值(可能是)分配给您的核心脚本。
回答by Kevin
BASH scans the file linearly and executes statements as it comes across them, just like it does when you're on the command line. There are two ways I see to do what you want. First, you could write the code-generating heredoc, etc. in a separate file (say helperfile.sh) and source it with . helperfile.sh. That's probably best. You could also write a function (mainperhaps) in the beginning that does what you want, then the heredoc code, then at the bottom call main.
BASH 线性扫描文件并在遇到它们时执行语句,就像在命令行上一样。我认为有两种方法可以做你想做的事。首先,您可以在单独的文件(比如 helperfile.sh)中编写代码生成 heredoc 等,并使用. helperfile.sh. 那可能是最好的。您也可以main在开始时编写一个函数(也许)来执行您想要的操作,然后是 heredoc 代码,然后在底部调用main.

