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

Wrapper script has some bashisms left #4410

Merged

Conversation

matiasgarciaisaia
Copy link
Member

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 on tput) 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 the if branch that used remove_path_item - and that was Bash-dependent.

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
Copy link
Contributor

@bew bew May 13, 2017

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 ?

Copy link
Member Author

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 (?)

Copy link
Contributor

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" ] 😃

Copy link
Member Author

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
Copy link
Contributor

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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bew this PR is basically a follow up to this one: #3809

Copy link
Contributor

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)

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Contributor

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/:$//'
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Contributor

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
Copy link
Contributor

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/:$//'
Copy link
Contributor

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.

@RX14
Copy link
Contributor

RX14 commented May 23, 2017

Please can we hurry this: on stock debian I get ./bin/crystal: 117: [: Illegal number: on master.

See: https://jenkins.crystal-lang.org/job/crystal/job/feature%252Fjenkinsfile/28/execution/node/17/log/

@matiasgarciaisaia matiasgarciaisaia merged commit 5aa3b15 into crystal-lang:master May 23, 2017
@matiasgarciaisaia
Copy link
Member Author

Thanks everyone for the feedback! 👍

@mverzilli mverzilli added this to the Next milestone May 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants