Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random text file in terminal

I am trying to generate a random text file on my macbook from terminal. What I am trying is that:

tr -dc A-Za-z0-9 < /dev/urandom | head -c100 > RandomFile.txt

but im getting

tr: Illegal byte sequence

What am i doing wrong?

like image 904
TheBlackCorsair Avatar asked Nov 24 '12 13:11

TheBlackCorsair


2 Answers

Set this environment variable and you should be good to go:

setenv LC_ALL C

The answer for which I found on this page.

And with that environment variable in place, I see a nicely formatted output:

tr -dc A-Za-z0-9 < /dev/urandom | head -c100
Kk4kfjR3O0UraMpfTGicGvYCziFClJQUTO3zCXdo05RTxEUigqPXTkjtiGOsTsaNyqNR3rX2dsmPlHkSdqO5qWBTmIFIYezsekWT[~]:;
like image 196
Michael Dautermann Avatar answered Sep 20 '22 15:09

Michael Dautermann


# Print or assign a random alphanumeric string of a given length.
# rndstr len [ var ]
function rndstr {
    if [[ $FUNCNAME == "${FUNCNAME[1]}" ]]; then
        unset -v a
        printf "$@"
    elif [[ $1 != +([[:digit:]]) ]]; then
        return 1
    elif (( ! $1 )); then
        return
    else
        typeset -a a=({a..z} {A..Z} {0..9})
        eval '${2:+"$FUNCNAME" -v} "${2:-printf}" -- %s "${a[RANDOM%'"${#a[@]}"']"{1..'"$1"'}"}"'
    fi
}

rndstr 100

This is my library function for this. The advantage is performance and the ability to assign to a variable directly. Might be overkill for you.

like image 39
ormaaj Avatar answered Sep 19 '22 15:09

ormaaj