Skip to content

Instantly share code, notes, and snippets.

@rubeniskov
Last active September 29, 2022 00:39
Show Gist options
  • Save rubeniskov/b039a9a37827421e7eb3981a29d36c01 to your computer and use it in GitHub Desktop.
Save rubeniskov/b039a9a37827421e7eb3981a29d36c01 to your computer and use it in GitHub Desktop.
Concatenate bash sources once to create standalone file
#!/usr/bin/env awk
# usage
# echo "main.sh" | awk -v header='# HEADER COMMENT' -v footer='# FOOTER COMMENT' -f concat-bash-sources.awk
function read_file_content(file) {
while ((getline line < file) > 0) {
if ( line ~ /^source / ) {
print parse_bash_source(line)
# remove comments and hashbangs
} else if ( line !~ /^#/ ) {
print line
}
}
}
function match_source_path(line) {
return substr(line, 9, length(line) - 9)
}
function parse_bash_source(line) {
src = match_source_path(line)
if (cached[src] != 1) {
read_file_content(src)
}
cached[src] = 1
}
BEGIN {
print "#!/usr/bin/env bash"
if (header) {
print header
}
}
{
read_file_content($0)
}
END {
if (footer) {
print footer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment