Debian Codegraph @

rnd.sh

raw symbols SCIP
1 #!/bin/bash
2 # rnd.sh: Outputs a 10-digit random number
3
4 # Script by Stephane Chazelas.
5
6 head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
7
8
9 # =================================================================== #
10
11 # Analysis
12 # --------
13
14 # head:
15 # -c4 option takes first 4 bytes.
16
17 # od:
18 # -N4 option limits output to 4 bytes.
19 # -tu4 option selects unsigned decimal format for output.
20
21 # sed:
22 # -n option, in combination with "p" flag to the "s" command,
23 # outputs only matched lines.
24
25
26
27 # The author of this script explains the action of 'sed', as follows.
28
29 # head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
30 # ----------------------------------> |
31
32 # Assume output up to "sed" --------> |
33 # is 0000000 1198195154\n
34
35 # sed begins reading characters: 0000000 1198195154\n.
36 # Here it finds a newline character,
37 #+ so it is ready to process the first line (0000000 1198195154).
38 # It looks at its <range><action>s. The first and only one is
39
40 # range action
41 # 1 s/.* //p
42
43 # The line number is in the range, so it executes the action:
44 #+ tries to substitute the longest string ending with a space in the line
45 # ("0000000 ") with nothing (//), and if it succeeds, prints the result
46 # ("p" is a flag to the "s" command here, this is different
47 #+ from the "p" command).
48
49 # sed is now ready to continue reading its input. (Note that before
50 #+ continuing, if -n option had not been passed, sed would have printed
51 #+ the line once again).
52
53 # Now, sed reads the remainder of the characters, and finds the
54 #+ end of the file.
55 # It is now ready to process its 2nd line (which is also numbered '$' as
56 #+ it's the last one).
57 # It sees it is not matched by any <range>, so its job is done.
58
59 # In few word this sed commmand means:
60 # "On the first line only, remove any character up to the right-most space,
61 #+ then print it."
62
63 # A better way to do this would have been:
64 # sed -e 's/.* //;q'
65
66 # Here, two <range><action>s (could have been written
67 # sed -e 's/.* //' -e q):
68
69 # range action
70 # nothing (matches line) s/.* //
71 # nothing (matches line) q (quit)
72
73 # Here, sed only reads its first line of input.
74 # It performs both actions, and prints the line (substituted) before
75 #+ quitting (because of the "q" action) since the "-n" option is not passed.
76
77 # =================================================================== #
78
79 # An even simpler altenative to the above one-line script would be:
80 # head -c4 /dev/urandom| od -An -tu4
81
82 exit