-
Notifications
You must be signed in to change notification settings - Fork 25
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
POSIX shell changes #12
Conversation
POSIX date does not support the %N format, also POSIX shell does not support the substring expansion format, or arrays. Another problem with the original script is the use of dd, which may not return the number of bytes expected (see https://unix.stackexchange.com/questions/121865/create-random-data-with-dd-and-get-partial-read-warning-is-the-data-after-the#121888) The original also did not format the resulting string like the other scripts in 8-4-4-4-6 format. I think it's fine to change the original script to specify bash as the shell not sh, but just to point out the issues, here's a stab at a posixly correct version? (shellcheck at least does not complain)
This implementation yields incorrect results:
|
in that case the original implementation is also incorrect, because it also depends on xxd |
This version will run on onecompiler and should still be posixly correct. (ie bourne shell not bash) Instead of taking the first n bytes out of urandom and converting them to hex (using xxd) the trick here is to take bytes dropping any that are not in the range of allowed hex characters. This still ends up with the right number of hex digits, tho it's not particularly efficient.
Figured out how to drop xxd from the script if I can use tr, which is also defined by POSIX. https://pubs.opengroup.org/onlinepubs/9699919799/nframe.html this works, but is a bit of a hack... (inevitable if you stick to bourne shell). I tried it in that onecompiler environment as well as locally and it works just fine. |
timestamp_lo is 16 bits but the mask only took 8.
Awesome! Thank you so much! |
It also does not support
from |
fair points all. Any suggestions for fixing it? We can use dd to replace head, but date's more of an issue. One possibility is awk: |
I wrote up a version here: https://posts.summerti.me/posix-compliant/ A fully POSIX approach is fairly painful and slow, so I'd recommend avoiding such considerations and just label it as Bash on GNU/Linux Additionally: |
POSIX date does not support the %N format, also POSIX shell does not support the substring expansion format, or arrays.
Another problem with the original script is the use of dd, which may not return the number of bytes expected (see https://unix.stackexchange.com/questions/121865/create-random-data-with-dd-and-get-partial-read-warning-is-the-data-after-the#121888)
The original also did not format the resulting string like the other scripts in 8-4-4-4-6 format.
I think it's fine to change the original script to specify bash as the shell not sh, but just to point out the issues, here's a stab at a posixly correct version? (shellcheck at least does not complain)