Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I wrote following script, which generate random password and store it in file

pass1=</dev/urandom tr -dc _A-Z-a-z-0-9|head -c8
echo $pass1
echo "$pass1" >/tmp/a

Above script printing generated password through "echo $pass1" command. But nothing getting stored inside created /tmp/a file .

Please let me know whats wrong i am doing.

share|improve this question
    
Are you missing a pipe between urandom and tr? –  jscott Jun 22 '12 at 15:57
    
@jscott, this is a unique usage of the < operator –  Mike Pennington Jun 22 '12 at 16:00
    
@MikePennington Thanks for that, I was trying to work through the syntax and what the expected output is... Both the OP's and your examples produce tr errors for me. –  jscott Jun 22 '12 at 16:03

1 Answer 1

up vote 1 down vote accepted

It's not necessary to store the password in an intermediate variable. You only need this line in your shell script

</dev/urandom tr -dc _A-Z-a-z-0-9|head -c8 > /tmp/a

I saved as foo.sh, made permissions 755, and executed it under linux...

[mpenning@chestnut ~]$ uname -a
Linux chestnut.he.net 2.6.32.46-1-grsec #1 SMP Fri Sep 2 12:42:23 PDT 2011 x86_64 GNU/Linux
[mpenning@chestnut ~]$ ./foo.sh
[mpenning@chestnut ~]$ cat /tmp/a
qAUezN0-[mpenning@chestnut ~]$
share|improve this answer
    
This gives me: tr: write error: Broken pipe tr: write error –  jscott Jun 22 '12 at 16:00
    
hmm works in cygwin and linux... see my edit –  Mike Pennington Jun 22 '12 at 16:01
    
Thanks , this worked for me </dev/urandom tr -dc _A-Z-a-z-0-9|head -c8 > /tmp/a –  vnix27 Jun 22 '12 at 16:09
    
Oh my, I'm still getting the tr errors, yet /tmp/a is created and contains the expected data. Odd, must be something screwy in my shell. +1 –  jscott Jun 22 '12 at 16:11
    
@vnix27, FYI... there is also the mkpasswd command on most linux systems, but I like the control tr gives me –  Mike Pennington Jun 22 '12 at 16:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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