I am trying to read from /dev/random and /dev/urandom and would like to know what is the best way to read from them and block/character special devices in general using bash shell scripting ?
bash check if string ends with character. shell script to check whether a character is alphabet digit or special character.
Bash Character Escaping. Except within single quotes, characters with special meanings in Bash have to be escaped to preserve their literal values. In practice, this is mainly done with the escape character \ <backslash>.
To read the Bash user input, we use the built-in Bash command called read. It takes input from the user and assigns it to the variable.
Use dd
to get blocks of data from the device. E.g. to get 8 bytes from /dev/urandom
:
dd if=/dev/urandom count=1 bs=8 | ...
Then you can use od
to convert the bytes to a human-readable form:
$ dd if=/dev/urandom count=1 bs=8 2>/dev/null | od -t x1 -A n
b4 bc 2f 59 dd 55 1b 4a
By the way, if you only need random numbers in bash, $RANDOM
is probably more useful:
$ echo $RANDOM $RANDOM $RANDOM $RANDOM
3466 6521 4426 9349
My hint:
dd if=/dev/urandom count=4 | ...
or e.g. The tail is heavily dependent on what you want to do with that data
To format as a long integer number:
dd if=/dev/urandom bs=1 count=4|od -l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With