LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2009, 12:08 PM   #1
jessjames
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
bash search and replace question


Hi

I need to achieve something very specific, and all the responses I find do not quite match my requirements. Therefore, if I am repeating a question I apologise.

I am trying to import many many users from one server to another. I can automate the user creation, but I need to copy over the passwords. To achieve this I'm planning to copy the hash from /etc/shadow on the old server to the same file on the new one.

I do not know the individual users' passwords from a plain text point of view, but I have confirmed that simply copying the hash over to the new shadow file maintains the password and the login works.

So what I'm trying to do is this :

I have a lookup list of usernames and hash's from the old server that looks like this :

Quote:
username $1$EK/hjwGC$MtKJj/EpCvj32Ay4v4FAi1
So... two fields seperated by a space.

I have a destination shadow file which might look like this :

Quote:
username:!!:14532:0:99999:7:::
...since I've created all my new users without passwords at the moment.

And so, my solution would involve searching for each individual username in the destination shadow file, (username is specified by field 1 in my lookup list), and on that line replace the string "!!" with the correct password hash (hash specified by field 2 in my lookup list).

I can pull out the two fields from my lookup list by doing something like :

Quote:
while read username passwordhash
do
echo "Username : " $username ", password : " $passwordhash
done < $SOURCEFILENAME
But I then need to use sed (or similar) to grep for one string, and on the resulting line replace another string with the new hash using the information from my lookup list for the search and replace strings, and without affecting the rest of the file.

I realise this is a very specific requirement, and there might be a better way to approach it altogether, but logically this seemed like a good solution. I'm just missing the scripting logic/commands.

Any suggestions on this would be awesome, since I'm not much of a bash guru, and it's driving me slightly insane

Many thanks in advance.
Jess
 
Old 10-28-2009, 12:20 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can see a similar example here. Just change the field numbers around.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace in bash script daemoneye Other *NIX 1 04-08-2009 02:24 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM
Bash search and replace utility cpedros Programming 3 03-12-2008 02:02 PM
Search and Replace from bash hammertime1983 Linux - Newbie 4 11-02-2007 04:18 PM
sed question for search and replace jakev383 Linux - General 8 05-05-2007 06:40 AM


All times are GMT -5. The time now is 12:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration