#!/bin/bash # rnd.sh: Outputs a 10-digit random number # Script by Stephane Chazelas. head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' # =================================================================== # # Analysis # -------- # head: # -c4 option takes first 4 bytes. # od: # -N4 option limits output to 4 bytes. # -tu4 option selects unsigned decimal format for output. # sed: # -n option, in combination with "p" flag to the "s" command, # outputs only matched lines. # The author of this script explains the action of 'sed', as follows. # head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' # ----------------------------------> | # Assume output up to "sed" --------> | # is 0000000 1198195154\n # sed begins reading characters: 0000000 1198195154\n. # Here it finds a newline character, #+ so it is ready to process the first line (0000000 1198195154). # It looks at its s. The first and only one is # range action # 1 s/.* //p # The line number is in the range, so it executes the action: #+ tries to substitute the longest string ending with a space in the line # ("0000000 ") with nothing (//), and if it succeeds, prints the result # ("p" is a flag to the "s" command here, this is different #+ from the "p" command). # sed is now ready to continue reading its input. (Note that before #+ continuing, if -n option had not been passed, sed would have printed #+ the line once again). # Now, sed reads the remainder of the characters, and finds the #+ end of the file. # It is now ready to process its 2nd line (which is also numbered '$' as #+ it's the last one). # It sees it is not matched by any , so its job is done. # In few word this sed commmand means: # "On the first line only, remove any character up to the right-most space, #+ then print it." # A better way to do this would have been: # sed -e 's/.* //;q' # Here, two s (could have been written # sed -e 's/.* //' -e q): # range action # nothing (matches line) s/.* // # nothing (matches line) q (quit) # Here, sed only reads its first line of input. # It performs both actions, and prints the line (substituted) before #+ quitting (because of the "q" action) since the "-n" option is not passed. # =================================================================== # # An even simpler altenative to the above one-line script would be: # head -c4 /dev/urandom| od -An -tu4 exit