LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2012, 01:27 AM   #1
fajarpri
LQ Newbie
 
Registered: May 2005
Posts: 9

Rep: Reputation: 0
Cool Help bash: parsing file into command


Hi all,
Can pls help me with this.
I have a file consisting:
Code:
user1:pass1
user2:pass2
user3:pass3

How do I create bash script to read the file and to be used with this command:
echo -e "new_password\nnew_password" | (smbpasswd -a -s $USER)
Thank you.
Fajar.

Last edited by fajarpri; 04-06-2012 at 05:21 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-06-2012, 02:33 AM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Greetingz!
Please edit your post and wrap the 'file consisting of...' section with [code] and [/code] tags.
That way, the emotes (little faces) don't pop-up.

Example:
Quote:
This is my output
[code]
output line one
output line two
output line three
[code]
Thank you
Also, the 'command' you mention appears to be part of a script. By chance could you post the whole script (using the 'code' tags)?

Last edited by xeleema; 04-06-2012 at 02:34 AM.
 
Old 04-06-2012, 03:04 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Provided your input is:
Code:
user1:pass1
user2:pass2
user3:pass3
a while read loop should do the trick:
Code:
OLD_IFS="$IFS"
IFS=":"
while read user pass
do
  echo User is $user
  echo Pass is $pass
done < file
IFS="$OLD_IFS"
The Input Field Separator is changed to let the read statement accept two fields simultaneously. It is restored to the original (saved) value outside the loop.
 
2 members found this post helpful.
Old 04-06-2012, 03:48 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hi colucix

colucix's technique is good. The same can be done in less lines with
Code:
while IFS=: read user pass
do
  echo User is $user
  echo Pass is $pass
done < file
 
2 members found this post helpful.
Old 04-06-2012, 05:16 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi catkin! Thank you for the suggestion. Definitively more plain and effective.
 
Old 04-06-2012, 05:38 AM   #6
fajarpri
LQ Newbie
 
Registered: May 2005
Posts: 9

Original Poster
Rep: Reputation: 0
Smile SOLVED: Help bash parsing file

Hi guys,
Much appreciated for the help. It works
So, the script is:
Code:
file=passlist.txt

while IFS=: read user pass
do
	echo user is $user
	echo pass is $pass
echo -e "$pass\n$pass" | (smbpasswd -s $user)
done < $file
Thank you so much,
Fajar.
 
  


Reply



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
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 05:37 PM
Wrong parsing of ls command in bash scripting itz2000 Programming 3 04-25-2007 10:23 AM
Help on parsing a log file in BASH globemast Programming 5 01-11-2007 01:56 AM
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM
Script File: Parsing command sent to shell cheema Programming 1 07-01-2005 12:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:37 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
Open Source Consulting | Domain Registration