1

I'm trying to generate random pass of the users created by a text file in a bash script and then saving those passwords into a text file.. That's how I'm doing..

It works fine and creates user from text file and output the password as well well to lol file, but the passwords for created users wont work.

Here's my code, any help regarding that would be appreciated..

while read line || [ -n "$line" ] # protect against no newline for last line

do name=$line
pass=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 8| head -n 1 >> lol)

sudo useradd $name -m
sudo usermod -p $(echo $pass | openssl passwd -1 -stdin) $name

echo "The user - $name - has been created!"

done < "$fname" # double-quote fname echo
3

The return value of command substitution is the stdout of the subshell.

In the following line, you've redirected the output to the file lol within the subshell. therefore there is no return value for you to store in the variable pass:

pass=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 8| head -n 1 >> lol)

You could either split the following line into two as follows:

pass=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 8| head -n 1)
echo "$pass" >> lol

Or use tee -a:

pass=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 8| head -n 1 | tee -a lol)
1
  • simply hilarious! thank you so much.. Splitting worked fine for me..
    – Umair
    Dec 1 '14 at 0:01

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.