i try run this code to get random number but eval seems not working this is my code


# Sequencia alfanumerica de 12 carateres aleatoriamente (maiusculas e minusculas)

randompasswd=$(cat /dev/urandom | tr -dc '1-9' | fold -w 1 | head -n 1 > $oncepass)
onlytail=$(tail -1 $oncepass)
pass=$(tail -1 $file)
testename=$(grep -R $onlytail $file)

if [ $onlytail != "$testename" ]
    echo "estou a ecoar para o file"
    echo $onlytail >> "$file"
    echo "já existe "
    echo "$onlytail"

eval $randompasswd
echo "$onlytail"
echo "fim"
exit 0


  1. if not exist number
estou a ecoar para o file
  1. If exist number
já existe 

They give me the same number. If anyone can help me i apreciatte

Sorry for my eng

  • Do you know that your computer may already have installed a command line program pwgen, which generates random passwords? The program has many options (see its man page), some of which might be of interest to you. Just saying, since the purpose of your script seems to be password creation. – user1404316 Apr 20 '18 at 12:22

The eval will do nothing since $randompasswd is empty.

You set randompasswd with

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
  • Works flawlesly. Thank you very much for the help. I know this is impossible to happen, but what if, in the lifetime of this script I generate every possible number? That was my biggest concern all along. How do I get out of this loop, if I ever end up doing, imagine, random 1 number from 1 to 9. after 9 tries I get stuck in the loop. Do you recommend anything for this matter? I'm not picturing this problem correctly. Thanks again for the help. It works like a charm. – helpme123 Apr 20 '18 at 10:52
  • Never mind. I was missing the else figure on the if statement. Thanks a lot for the help. __ – helpme123 Apr 20 '18 at 10:56
  • @helpme123 I'm not sure which variation in my answer you are referring to, but they all generate 12 character passwords (as per the comment in your code). You would have to generate several thousands of them to finally start generating duplicates. – Kusalananda Apr 20 '18 at 10:58
  • You answer works flawlesly. I was curious how would it be just generating random numbers with 1 digit from 1 to 9. But yes, I'm gonna use your solution for a project I'm having at school. You saved me. thanks again. – helpme123 Apr 20 '18 at 11:00
  • @helpme123 To generate a single random digit you may also use $(( RANDOM % 10 )). – Kusalananda Apr 20 '18 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.