# Bash function to generate the random alphanumeric string with provided length. randomString() { DEFAULT_LENGTH=6 # if the parameter is not null, apply new length if [ ! -z "$1" -a "$1" != " " ]; then LENGTH=$1 else LENGTH=$DEFAULT_LENGTH fi echo $LENGTH; LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w $LENGTH | head -n 1 }
Test One:
MyRadomString=$(randomString) echo "My Radom String: '$MyRadomString'";
# Output:
My Radom String: 'a2d6sd';
Test Two:
MyRadomString=$(randomString 8) echo "My Radom String2: $MyRadomString";
# Output:
My Radom String2: 'S2sP6sk7';
No comments:
Post a Comment
Please post any queries and comments here.