Skip to content

Instantly share code, notes, and snippets.

@paulgibbs
Created December 30, 2016 20:25
Show Gist options
  • Save paulgibbs/9e3207fd912f84935d9bc888ae08beaa to your computer and use it in GitHub Desktop.
Save paulgibbs/9e3207fd912f84935d9bc888ae08beaa to your computer and use it in GitHub Desktop.
WHSHASCRIPT="sha1sum" && (type sha1sum >/dev/null 2>&1 || WHSHASCRIPT="shasum")
@henrywright
Copy link

If the type sha1sum >/dev/null 2>&1 command succeeds, WHSHASCRIPT="shasum" will never be executed.

I may be wrong but you seem to be redirecting the output of type sha1sum into the black hole /dev/null and sending errors to STDOUT. My guess is that is all succeeding and WHSHASCRIPT="shasum" is never run.

@paulgibbs
Copy link
Author

@henrywright Not quite true. If you simply that and do something like type sha1sum >/dev/null 2>&1 || echo "this will get printed", then that message will get printed. You should be able to test this and see on any Unix-like shell (just replace sha1sum with some made-up app name that you don't have).

@paulgibbs
Copy link
Author

Oh, I've got this. From memory, those curly brackets evaluate inside another (nested) shell, and I bet that value is being set inside that transient shell.

Removing the curly brackets make it behave like I think it should. I might need to replace with square brackets; next step to figure out how to get it working in my script!

@henrywright
Copy link

The subshell theory is a good shout @paulgibbs. Try exporting the WHSHASCRIPT variable to make it also available in the child shell

@henrywright
Copy link

Also I agree my original guess is wrong. I assumed you had sha1sum on your Mac, which of course you don't.

@paulgibbs
Copy link
Author

paulgibbs commented Jan 1, 2017

For the record, I've iterated on this approach; I was using this to find which sha-1 tool exists (mac vs most linux) - and then run it - but i've switched approach to using the openssl program, like this: cat %2$s | openssl dgst -sha1 -binary | xxd -p

@henrywright
Copy link

Only problem I can see with using that program is how can you guarantee the binary is in the user's path?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment