eval will do nothing since
$randompasswd is empty.
randompasswd=$(cat /dev/urandom | tr -dc '1-9' | fold -w 1 | head -n 1 > $oncepass)
The command within
$(...) produces no output (because the output is redirected to a file instead), so
$randompasswd will be an empty string.
This is good, because I suppose you don't actually want to run
eval on a random string of characters?
Apart from that, there are a number of unquoted variable expansions that should be double quoted, for example
$onlytail in the
if-statement (if it contains spaces, there would be a syntax error there).
I can't say much else as I don't know what it is you want to do with this code.
To create a stream of passwords consisting of 12 alphanumeric characters:
tr -dc '[:alnum:]' </dev/urandom | fold -w 12
To pick the first one of these:
tr -dc '[:alnum:]' </dev/urandom | fold -w 12 | head -n 1
If you want to generate 100 unique passwords (and be sure that these are unique):
while true; do
tr -dc '[:alnum:]' </dev/urandom | fold -w 12 | head -n 100 | sort -u -o password.list
if [ "$( wc -l <password.list )" -eq 100 ]; then
This first generates 100 passwords and sorts them (removing duplicates). Then it tests whether the resulting file has 100 lines. If not, it does the whole thing again, otherwise it exits the loop. The file
password.list will be overwritten if it exists.
If you have an existing
password.list file and you want to generate (and add) a password not already in that file:
while true; do
newpassword=$( tr -dc '[:alnum:]' </dev/urandom | fold -w 12 | head -n 1 )
if ! grep -q -F "$newpassword" password.list; then
printf 'New password generated: %s\n' "$newpassword"
printf '%s\n' "$newpassword" >>password.list