LDP/LDP/guide/docbook/abs-guide/rnd.sh

83 lines
2.5 KiB
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/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
2005-03-21 13:51:11 +00:00
# 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 <range><action>s. The first and only one is
2001-07-10 14:25:50 +00:00
# range action
# 1 s/.* //p
2005-03-21 13:51:11 +00:00
# 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
2006-12-20 21:11:55 +00:00
# ("p" is a flag to the "s" command here, this is different
#+ from the "p" command).
2001-07-10 14:25:50 +00:00
2005-03-21 13:51:11 +00:00
# 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).
2001-07-10 14:25:50 +00:00
2006-12-20 21:11:55 +00:00
# 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 <range>, so its job is done.
2001-07-10 14:25:50 +00:00
2005-03-21 13:51:11 +00:00
# In few word this sed commmand means:
# "On the first line only, remove any character up to the right-most space,
#+ then print it."
2001-07-10 14:25:50 +00:00
# A better way to do this would have been:
# sed -e 's/.* //;q'
# Here, two <range><action>s (could have been written
# sed -e 's/.* //' -e q):
# range action
# nothing (matches line) s/.* //
# nothing (matches line) q (quit)
2005-03-21 13:51:11 +00:00
# Here, sed only reads its first line of input.
2006-12-20 21:11:55 +00:00
# It performs both actions, and prints the line (substituted) before
#+ quitting (because of the "q" action) since the "-n" option is not passed.
2001-07-10 14:25:50 +00:00
# =================================================================== #
2005-03-21 13:51:11 +00:00
# An even simpler altenative to the above one-line script would be:
2001-07-10 14:25:50 +00:00
# head -c4 /dev/urandom| od -An -tu4
2008-11-23 22:43:47 +00:00
exit