2
random-string()
{
    cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-8} | head -n 1
}

Using this script to generate a list of several thousand random 8 digit alphanumeric passwords.

Is there any way I can add to this script to save the generated passwords to a .txt file?

I'm kinda new to Linux and have found many resources on the web but I'm stumped on this one.

3
  • 1
    Add > file.txt at the end.
    – Dzero
    Aug 1, 2015 at 16:18
  • 1
    @ArK Better a 1>> ;)
    – A.B.
    Aug 1, 2015 at 16:49
  • @A.B. Agreed :)
    – Dzero
    Aug 1, 2015 at 17:55

2 Answers 2

2

Using I/O Redirection

your_script 1>> pwd_list

to redirect and append stdout to file pwd_list.


E.G.

#!/bin/bash
random-string()
{
    tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w "${1:-8}" | head -n 1
}

if [ -f out_pwd ]; then
   rm out_pwd
fi

COUNTER=0
while [ "$COUNTER" -le 10 ]; do
    random-string "$@" 1>> pwd_list
done

exit 0

and simply start the script, the output into the file pwd_list is part of the script


or use this script, to redirect the output in the file pwd_list when you start the script

#!/bin/bash
random-string()
{
    tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w "${1:-8}" | head -n 1
}

COUNTER=0
while [ "$COUNTER" -le 10 ]; do
    random-string "$@"
done

exit 0

and start the script with

script_name 1>> pwd_list
4
  • Still having trouble. Could I get you to post the whole script as it should look? I would be forever greatful.
    – jkwages
    Aug 1, 2015 at 16:43
  • Sure :) and done =)
    – A.B.
    Aug 1, 2015 at 16:46
  • Nm. Got it working now. Ty so much.
    – jkwages
    Aug 1, 2015 at 16:47
  • @jkwages As you're a reputation 1 user: If this answer helped you, don't forget to click the grey at the left of the answer, which means "yes, this answer is valid"! ;-)
    – A.B.
    Aug 1, 2015 at 16:47
1

You have defined a function random-string() to generate random passwords containing A-Za-z0-9 and outputting only the topmost one (head -1), from your question it is not clear that do you want to save the passwords generated after one run each time to a file or you want to do this recursively.

Let me simplify your command first :

tr -dc '[:alnum:]' </dev/urandom | fold -w 8 | head -n 1
  • tr takes the character class [:alnum] which denotes characters A-Za-z0-9

  • There is no need for cat, pass the filename via STDIN to tr

  • Parameter expansion fold -w ${1:-8} is not needed, just use fold -w 8

Now to save the output after one run of the function (only one password), this will overwrite the file every time, use the shell > redirection operator :

$ random_string () { tr -dc '[:alnum:]' </dev/urandom | fold -w 8 | head -n 1 ;} >foobar

$ random_string

$ cat foobar 
MqJdehaO

We are saving the STDOUT of the function random_string to the file foobar in the current directory.

In the next run the file will be overwritten with the new content :

$ random_string

$ cat foobar 
NP85OmWw

To overwrite the file e.g. spamegg use >> redirection which means the STDOUT of the function will be appended to the file every time we run it :

$ random_string () { tr -dc '[:alnum:]' </dev/urandom | fold -w 8 | head -n 1 ;} >>spamegg

$ random_string

$ cat spamegg 
vjwOe6TB

$ random_string

$ cat spamegg 
vjwOe6TB
TBkv6qPh

To make it more flexible we can define how many passwords we want to save in one run by providing the number via number as the first argument to the function :

$ random_string () { tr -dc '[:alnum:]' </dev/urandom | fold -w 8 | head -n "$1" ;} >foobar

$ random_string 5

$ cat foobar 
AV79dZeC
byitWYfX
eDBAldiI
6OxNU78H
gRs7SwEO

As you can see 5 passwords have been generated and saved in the file foobar, change 5 with whatever number of passwords you want.

5
  • 1
    Ok, so it looks even better =)
    – A.B.
    Aug 1, 2015 at 17:02
  • And if i only want lowercase and numbers what takes the place of "allnum" ?
    – jkwages
    Aug 1, 2015 at 18:41
  • @jkwages In that case use '[[:lower:][:digit:]]' i.e. tr -dc '[[:lower:][:digit:]]' </dev/urandom | fold -w 8 | head -n 1
    – heemayl
    Aug 1, 2015 at 18:44
  • Wonderful. So glad I found askubuntu. You guys are so nice.
    – jkwages
    Aug 1, 2015 at 18:45
  • @jkwages We all are glad we could help you..also note that please select any one of the answers given as accepted by clicking the tick mark on the left of that answer so that this issue can be marked as solved..and optionally you can give an upvote (^ sign on the left of the answer)..this is how we appreciate others in Askubuntu..
    – heemayl
    Aug 1, 2015 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .