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

expr is sometimes the only option #15

Closed
ptman opened this issue Oct 22, 2013 · 3 comments
Closed

expr is sometimes the only option #15

ptman opened this issue Oct 22, 2013 · 3 comments

Comments

@ptman
Copy link

ptman commented Oct 22, 2013

VERSION=$(expr "$VERSION" : '.*"\(1.[0-9\.]*\)["_]')
          ^-- expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].

In this case the regular expression cannot be handled by the suggested mechanisms.

expr "$VERSION" \< 1.2 >/dev/null && continue
^-- expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].

expr can compare floats

@koalaman
Copy link
Owner

On Bash, Ksh and Zsh, you can use [[ ]], but you're right in that there's no POSIX sh way of doing that. Shellcheck should probably not suggest this if you're using : and have specified #!/bin/sh

As for floats, POSIX specifically states that string comparison should be used when the arguments are not integers, unlike $(( .. )) where support for floats is allowed. Which shell/environment are you running this on?

@ptman
Copy link
Author

ptman commented Oct 28, 2013

I try to write POSIX /bin/sh scripts, for which some of the messages given by shellcheck are very helpful. You are correct that I could use string comparisons. And anyway there are lots of version numbers for which float (and string) comparison fails. I should probably use dpkg --compare-versions, but that's only available on Debian-derived systems ;)

@koalaman
Copy link
Owner

koalaman commented Nov 3, 2013

From 8d265aa, Shellcheck doesn't warn about expr when using :

@koalaman koalaman closed this as completed Nov 3, 2013
@dengyufei123 dengyufei123 mentioned this issue May 19, 2023
4 tasks
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

2 participants