Skip to content

Commit

Permalink
Allow Shebang (#!) line mikefarah#1851
Browse files Browse the repository at this point in the history
Fairly dirty way of allowing shebang lines

This snippet will read the first two characters of a file passed in to
`yq` and remove the line if it begins with "`#!`". This will allow `yq`
to continue parsing it as though the shebang line was not there.

I was not able to find canonical documentation that the shebang is
limited to a single line or cannot include continuation characters.
However these seem to be accepted truths. See:

https://linux.die.net/man/2/execve

https://en.wikipedia.org/wiki/Shebang_(Unix)#Syntax
  • Loading branch information
rlee-arx committed Nov 16, 2023
1 parent bbe196b commit 89e7b97
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ func processArgs(originalArgs []string) (string, []string, error) {
if err != nil {
return "", nil, err
}

//detect and trim shebang (`#!`) line
if string(expressionBytes[0:2]) == "#!" {
eos := slices.Index(expressionBytes, '\n')
expressionBytes = expressionBytes[eos+1:]
}

expression = string(expressionBytes)
}

Expand Down

0 comments on commit 89e7b97

Please sign in to comment.