Bash - writing function definition in script after first call (as a GOTO/jump problematics)

佐手、 提交于 2019-11-30 09:15:38

A common technique is:

#!/bin/sh

main() {
  cmd_list
}

cat > file1 << EOF
big HEREDOC
EOF

main

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 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 (main perhaps) in the beginning that does what you want, then the heredoc code, then at the bottom call main.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!