3 ms·
Ha, turns out I just wrote this helper function a few weeks ago, inspired by Perl and Ruby: #!/usr/bin/env bash # read data starting from the provided
by lloeki 3y ago
Ha, turns out I just wrote this helper function a few weeks ago, inspired by Perl and Ruby:
#!/usr/bin/env bash
# read data starting from the provided section marker up to the next one or EOF
function section() {
local section="$1"
local source="${BASH_SOURCE[0]}"
awk '/^__[A-Z0-9]+__$/{f=0} f{print} /^'"${section}"'$/{f=1}' "${source}"
}
section __JSON__ | jq
section __YAML__ | ruby -ryaml -e 'p YAML.load(STDIN.read)'
exit
__JSON__
{ "a": 1 }
__YAML__
b:
- 1
- 2
- 3
My only wish is that shellcheck had a directive to stop yelling at me starting at a certain line.
Usually I augment it with such functions for clarity:
# whatever raw data
function data() {
section __DATA__
}
# man/perldoc like
function doc() {
section __DOC__
}
# command line help
function help() {
section __HELP__
}
- TacticalCoder 3y ago> My only wish is that shellcheck had a directive to stop yelling at me starting at a certain line. But it does no? Shellcheck can be configured to ignore SC by putting a "disable" directive in a config file or even as a comment in your Bash source file. It would work if it's always the same annoying warning / SCxxx message you get.
- lloeki 3y agoDirectives apply line by line. The only ones that apply to a bigger scope are mandated to be the next line right after shebang and apply to the whole file. https://github.com/koalaman/shellcheck/wiki/Directive https://github.com/koalaman/shellcheck/wiki/Directive > Directives that replace or are immediately after the shebang apply to the entire script. Otherwise, they are scoped to the command that follows it https://github.com/koalaman/shellcheck/wiki/Ignore https://github.com/koalaman/shellcheck/wiki/Ignore > Note that the directive must be on the first line after the shebang with versions before 0.4.6. As of 0.4.6 comments and whitespace are allowed before file-wide directives.
- lloeki 3y agoThere's an issue already about it: https://github.com/koalaman/shellcheck/issues/760 https://github.com/koalaman/shellcheck/issues/760