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?
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[~]:;
# 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With