Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ranges can only match single char #3140

Open
xubuntu4iran opened this issue Feb 6, 2025 · 7 comments
Open

Ranges can only match single char #3140

xubuntu4iran opened this issue Feb 6, 2025 · 7 comments

Comments

@xubuntu4iran
Copy link

xubuntu4iran commented Feb 6, 2025

~/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

@brother
Copy link
Collaborator

brother commented Feb 6, 2025

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.

@sideeffect42
Copy link

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

@brother
Copy link
Collaborator

brother commented Feb 7, 2025

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.

@sideeffect42
Copy link

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.

@xubuntu4iran
Copy link
Author

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

@xubuntu4iran
Copy link
Author

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

@sideeffect42
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants