shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Ranges can only match single char

Open xubuntu4iran opened this issue 1 year ago • 7 comments

~/Documents/Math/shellcheck-v0.10.0/shellcheck rep-factors.sh 

In rep-factors.sh line 41:
comp=$(sed -n "${j}p" "${filename}" |  jq -r  .[\"input-decimal\"]);
                                               ^-----------------^ SC2102 (info): Ranges can only match single chars (mentioned due to duplicates).

For more information:
  https://www.shellcheck.net/wiki/SC2102 -- Ranges can only match single char...

v0.10.0 stable release used I used sed to extract a line from a file which is a json object, so I used jq to extract json element

xubuntu4iran avatar Feb 06 '25 13:02 xubuntu4iran

I understand what you are reaching towards but this is frankly speaking a pretty lazy and bad issue description.

Ignore the problem with a directive.

brother avatar Feb 06 '25 20:02 brother

Please do not ignore this shellcheck message. It is completely valid and refers to a bug in your shell script.

Please quote your strings properly, single quotes don't hurt. In your code .["input-decimal"] is interpreted as a shell glob.

Example:

touch .i
echo '{"i": "hello bug"}' | jq -r .[\"input-decimal\"]
# prints hello bug

sideeffect42 avatar Feb 07 '25 08:02 sideeffect42

It is completely valid and refers to a bug in your shell script.

We are still guessing though as the issue description is not clear. To me this report is invalid and should be closed.

brother avatar Feb 07 '25 08:02 brother

We are still guessing though as the issue description is not clear.

The intent is pretty clear I think. @xubuntu4iran wants to extract the field "input-decimal" from the JSON object on line $j.

But because he doesn't quote this JQ code correctly it will be interpreted as a glob by the shell and it will blow up in his face if by chance any file matching .[input-decml"] exists. It's pure luck his code seems to work.

To me this report is invalid and should be closed.

Agreed. 100% not a bug in shellcheck.

sideeffect42 avatar Feb 07 '25 08:02 sideeffect42

Description added to first post, @sideeffect42 how to edit my script if it is not a bug

xubuntu4iran avatar Feb 09 '25 06:02 xubuntu4iran

Please do not ignore this shellcheck message. It is completely valid and refers to a bug in your shell script.

Please quote your strings properly, single quotes don't hurt. In your code .["input-decimal"] is interpreted as a shell glob.

Example:

touch .i echo '{"i": "hello bug"}' | jq -r .["input-decimal"]

prints hello bug

for me , it printed null echo '{"i": "hello bug"}' | jq -r .["input-decimal"] null

xubuntu4iran avatar Feb 09 '25 11:02 xubuntu4iran

Description added to first post, @sideeffect42 how to edit my script if it is not a bug

@xubuntu4iran: like I had written already, you have to quote your JQ code:

comp=$(sed -n "${j}p" "${filename}" | jq -r '.["input-decimal"]')

Example: touch .i echo '{"i": "hello bug"}' | jq -r .["input-decimal"]

for me , it printed null echo '{"i": "hello bug"}' | jq -r .["input-decimal"] null

You have to run the complete example I posted. First: touch .i And then: echo '{"i": "hello bug"}' | jq -r .[\"input-decimal\"]

Or run just touch .i and then your code and you will see it misbehave.

sideeffect42 avatar Feb 09 '25 13:02 sideeffect42