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
Wrapper script has some bashisms left #4410
Wrapper script has some bashisms left #4410
Conversation
The Docker image doesn't have tput available
There were some bashisms left that became a bug since crystal-lang#3809
bin/crystal
Outdated
} | ||
|
||
############################################################################## | ||
|
||
__has_colors() { | ||
local num_colors=$(tput colors 2>/dev/null) | ||
|
||
if [ "$num_colors" -gt 2 ]; then | ||
if [ "0$num_colors" -gt 2 ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this 0
in front of $num_colors
for ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be a bit too hacky, yes.
The idea is that, if tput
is not available (like when building in Docker), then $num_colors
would be empty - and the [
will fail. This way, if $num_colors
is empty, the string gets to be "0"
, while if it's not empty, "256"
turns into "0256"
- and still works.
I can change that to be a different test - ie, if it's empty, assume no color; else test for $num_colors
- but I should get the dust off my shell scripting book (?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see the trick! I think it's better to avoid tricks, readability should be king everywhere.
To check for empty string, you can use [ -z "$num_colors" ]
😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just fixed this 👍
@@ -1,4 +1,4 @@ | |||
#!/usr/bin/env sh | |||
#!/bin/sh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure which is the best, the old one is more portable, as sh
is not always in /bin/
on all systems (even if it's unlikely).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. My comment is still valid (but could be discussed somewhere else if needed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought /bin/sh
was actually required by POSIX or something like that.
Is there any system in which you don't have /bin/sh
available, but you have /usr/bin/env
and sh
in some other path?
I'm totally for rolling back to /usr/bin/env sh
if it is true that /usr/bin/env
is more standard than /bin/sh
- but I wouldn't think that's true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, checkbashisms
won't recognize the script as an sh
script if you keep the /usr/bin/env sh
shebang.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per this stackoverflow answer this is fine:
There are two programs whose location you can rely on on almost every unix variant: /bin/sh and /usr/bin/env.
work="${work#:}" | ||
|
||
echo "$work" | ||
echo -n $path | awk -v RS=: -v ORS=: '$0 != "'$item'"' | sed 's/:$//' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't have a dependency to awk yet. Is it safe to assume it will be there on all distros? Is there no other lighter approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came to it through this guide and the answer I put in the comment.
It doesn't seem doable without awk
/sed
/perl
/tr
- and I think awk
is the less rare of them. But we can totally change it.
I don't have much experience with this minimalist systems - I've just targeted the Docker build and made sure it worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sed
and awk
are the most portable of these (they're in posix) so this should be fine also.
@@ -1,4 +1,4 @@ | |||
#!/usr/bin/env sh | |||
#!/bin/sh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per this stackoverflow answer this is fine:
There are two programs whose location you can rely on on almost every unix variant: /bin/sh and /usr/bin/env.
work="${work#:}" | ||
|
||
echo "$work" | ||
echo -n $path | awk -v RS=: -v ORS=: '$0 != "'$item'"' | sed 's/:$//' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sed
and awk
are the most portable of these (they're in posix) so this should be fine also.
Please can we hurry this: on stock debian I get See: https://jenkins.crystal-lang.org/job/crystal/job/feature%252Fjenkinsfile/28/execution/node/17/log/ |
Thanks everyone for the feedback! 👍 |
In #3809, the
bin/crystal
wrapper script changed from bash to sh, but there were some bashisms left. I've removed them (and the dependency ontput
) so we can better support Alpine and any toaster brand that doesn't have Bash built-in :)In particular, running
docker build .
stopped working, because it went through theif
branch that usedremove_path_item
- and that was Bash-dependent.