0

Script take values from text file or excel (excel is better choice). file contains three column

username    old password   new password
lg1211      cSR121         csd22
lf1245      fDFss          sfdge
ms1232      nHJaa          hYiao

script

# !/bin/bash
file=/root/Desktop/Scripts/login
# location of file
while read line
do
username=$(echo $line | awk '{print $1}')
oldpassword=$(echo $line | awk '{print $2}')
newpassword=$(echo $line | awk '{print $3}')
passwd $username

done<$file

my quires is how it will take take oldpassword and newpassword after script execute this command passwd $username

  • Working with an excel document is going to be much harder from the shell than working with a csv or other text file. What are you actually trying to *do* in the script your "Algo" doesn't actually show any work? – Etan Reisner Jul 01 '15 at 12:42
  • @EtanReisner actually I want to know syntax, like by using AWk command or any other command , how to extract username, old password and new password from file and change password for all users till EOF, Text file will be ok for me – Manish Tiwari Jul 01 '15 at 12:47
  • Pick an exact file format to work with and then try it with awk or perl or whatever else you are planning on actually doing work with later. We can't help with a general "how do I parse a file" question. – Etan Reisner Jul 01 '15 at 13:16
  • See this page https://www.debian-administration.org/article/668/Changing_a_users_password_inside_a_script then simply read the file and pass the values to usermod – Vorsprung Jul 01 '15 at 14:42
  • @EtanReisner Please help now, I edit the query with script – Manish Tiwari Jul 01 '15 at 15:44
  • Those `echo` commands aren't safe for spaces in passwords (but then again neither is `awk` used like that). But you don't need `echo | awk` here if you are already using `read` since it will split on spaces into variables for you. See [Bash FAQ 001](http://mywiki.wooledge.org/BashFAQ/001) for reading field-by-field. – Etan Reisner Jul 01 '15 at 15:50

1 Answers1

0

If you have admin rights is better use chpasswd command:

# !/bin/bash
file=/root/Desktop/Scripts/login
# location of file
while read line
do
    echo $line | awk '{print $1 ":" $3}' | chpasswd  
done<$file

If you do not have admin rights you should use "expect", look this link Script to change password on linux servers over ssh

Community
  • 1
  • 1