1

I want to grep the users (first column) from the /etc/passwd file and assign them a password. I only want to do this for user accounts and not default system accounts. So far I have a simple bash script

#!/bin/bash
touch /usertempfile
grep "/home" /etc/passwd |grep -v "/bin/false"|grep -v "/sbin/nologin"|grep -v $USER |awk -F":" '{print $1}' >> /userstempfile
for i in $(cat /usertempfile); do
    echo -e "password/npassword" |passwd $i
done
rm -rf /usertempfile

The script is able to extract the users from the /etc/passwd file that are not a default system account but is unable to change the passwords of the users which are in that file. What am I doing incorrectly?

  • 1
    Hm, is that really echo -e "password/npassword" in your script and not \n? – Ulrich Schwarz Dec 24 '16 at 7:08
2

I am using chpasswd in my script and it's working fine for me:

echo "$USERNAME:$NEWPASSWORD" | chpasswd

PS: for not root user use sudo before chpasswd

echo "$USERNAME:$NEWPASSWORD" | sudo chpasswd

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.