Bash – 在第一次调用后在脚本中编写函数定义(作为GOTO /跳转问题)

我基本上想给我写一个bash脚本,在那里我使用 heredoc生成几个大文件;然后使用这些文件运行一些命令.

据了解,(显然)heredoc文件需要在命令运行之前生成 – 但是,在这种安排中令我恼火的是,在编写命令代码之前,我还必须编写’heredoc’语句代码.

所以我以为我会在一个函数中编写heredoc语句 – 但这里仍然存在同样的问题:Chapter 24. Functions说:

The function deFinition must precede the first call to it. There is no method of “declaring” the function,as,for example,in C.

确实如此:

$cat > test.sh <<EOF
testo

function testo {
  echo "a"
}
EOF

$bash test.sh 
test.sh: line 1: testo: command not found

然后我想也许我可以放置一些标签并用GOTO跳转,如(伪代码):

$cat > test.sh <<EOF
goto :FUNCLABEL

:MAIN
testo

goto :EXIT

:FUNCLABEL
function testo {
  echo "a"
}
goto MAIN

:EXIT

…但事实证明BASH goto也不存在.

我唯一的目标是 – 我想首先编写脚本文件的“核心”,这是一些五六个命令;然后才在脚本文件中写入heredoc语句(可能有数百行);首先拥有heredocs确实让我难以阅读代码.有没有办法实现这一目标?

一种常见的技术是:
#!/bin/sh

main() {
  cmd_list
}

cat > file1 << EOF
big HEREDOC
EOF

main

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...