Change password via bash script

Hello,
I have a question how can i change user password in bash script, this doest work (i run this script as root):

Code:
#!/usr/local/bin/bash
$(passwd foo) <<EOF
pass
pass
EOF

Is it possible to do this via bash script?
 
Read pw command man page and look for -h FD option. Add user called foo and set password to BAR
Code:
echo BAR | pw add user foo -h 0
To modify password for existing user called tom:
Code:
echo jerry | pw mod user tom -h 0
 
Thx for this. But i need this via passwd :/. In other words i need to type a password via script. But script must be non parametrs.
 
Hi, sorry for a big delay. This script will set password by passwd without user interaction:

Code:
#!/usr/local/bin/bash
LOG=tomek
PASS=haslo1
expect << EOF
spawn passwd $LOG
expect "New Password:"
send "${PASS}\r"
expect "Retype New Password:"
send "${PASS}\r"
expect eof;
EOF

This will set password "haslo1" for user "tomek".
 
Back
Top