Last active
September 16, 2015 14:47
-
-
Save stevekm/be74f443d5dfffe5ed5c to your computer and use it in GitHub Desktop.
Generate a random string of letters and numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# I needed a unique random name for files generated by scripts | |
# so they don't get overwritten on subsequent compiles | |
# use mktemp to generate a randomly named tmp directory, save the name to a variable | |
BASE=$(mktemp) | |
echo $BASE # the output looks like this: /tmp/tmp.XMbgXwwviZ | |
# we only want the base random string, not the file path | |
# use these two steps to strip out the file path | |
BASE2=${BASE///tmp} | |
BASE3=${BASE2//./} | |
echo $BASE3 # the output looks like this: XMbgXwwviZ | |
# we can now use $BASE3 in the filename of files created by scripts | |
echo "This is a file" > ~/"$BASE3".txt | |
cat ~/"$BASE3".txt # This is a file | |
# in order to preserve it for use later in the script, we will save it | |
echo $BASE3 > ~/BASE3_tmp.txt | |
# note that this is using "BASE3.txt" and not "$BASE3.txt"; | |
# this is so that a script will be able to find the entry | |
# later, we can retrieve it | |
BASE4=$(cat ~/BASE3_tmp.txt) | |
echo $BASE4 # XMbgXwwviZ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment