[Rustington] Random strings

david david at m1.arnstein.name
Wed Apr 14 17:01:31 UTC 2021


Hi All,

I started trying to explain the random string generating commands/code
that I mentioned in the meeting and got adding about lines getting
broken up in emails causing trouble with making the code work. I now
realise the comment lines themselves might get broken up so that the #
symbol would not then be at the start of the line where it should be ...

So below assume all lines are comment lines until you get to the line
starting:

#!/bin/bash

which is a special one (see explanation). After that is some code which
is on short lines to avoid the email line splitting problem.
The alternative code right at the end should be self explanatory -
email me if it isn't.

# Here is a way of creating random strings of characters of
# fixed length.

# Create more (hopefully) than enough random strings
# sort and remove duplicates (if any)
# then truncate list at 500,000

# I have started the code with the time command so we can
# see how long it takes on various computer if you are
# interested to try.

# Also I have split the commands up between the | 
# symbols to make things clearer (email would have broken
# the lines in an arbitrary way which then would not
# have worked) but all the # commands can be on one
# line if you like. (Lines starting with # are comment
# lines and are ignored except if the first line starting
# with # then has ! the rest of the line is take to be
# the file path to the interpreter/shell/program that
# is then started and given the following lines to execute).

# tr -dc reads from a pseudo device that generates random
# characters drops any unwanted characters
# ie only accepts 'a-zA-Z0-9'

# This output is then piped into fold that inserts a newline
# every 12 characters

# That is then piped into head to stop the process
# at 501000 lines

# Head pipes the data into sort -u which sorts the lines
# and the -u causes only unique lines to be output

# You would then normally redirect the output into a file
# with > file
# but here I have piped it into head again so that you see
# only the first 10 lines.

# You can try this out just typing into a terminal the lines
# below starting tr

# Or if you put the lines starting #!/bin/bash
# into a file (say called make-strings)
# and make it executable with chmod +x make_strings
# you can run it with ./make_strings

# I have started with the time command so we can see
# how long the process takes.

# On my machine it takes about 1.7 seconds elapsed time.
# It will be interesting to see how long it takes other people
# although the time will depend on whether or not your other,
# including background, tasks are already taking a lot of resources
# at the same time.

#!/bin/bash
time tr -dc 'a-zA-Z0-9' < /dev/urandom |
fold -w 12 |
head -501000 |
sort -u |
head -500000 |
head
# or you could do
# head -500000 > mydata
# and then
# head mydata to view the first ten lines





More information about the Rustington mailing list