LinuxQuestions.org
Register a domain and help support LQ
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 04-02-2012, 06:18 AM   #1
rudra_biswal
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
How to fetch data from file


Hello All,
I am having a file named "data.txt".It has two content
name=kevin
password=xxxxx

I want to crate a script.....when I will go for name it will fetch name from "data.txt". and when I will go for password it will fetch password from "data.txt".

How to do that?

n.b :I know how to fetch only 1 data from file by using the for loop.
 
Old 04-02-2012, 06:41 AM   #2
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,629

Rep: Reputation: Disabled
I aint a bash scripting guru or expert. But if you could attach the file here for the format, you could get better help. Have you tried to use awk in some way?
 
Old 04-02-2012, 07:53 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
What have you tried so far and can you explain "when I will go for name"?
 
Old 04-02-2012, 08:22 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
In very simple terms
Code:
n=$(grep name data.txt|cut -d'=' -f2)
# do something with $n ie name

p=$(grep password data.txt|cut -d'=' -f2)
# do something with $p ie passwd
 
Old 04-03-2012, 04:02 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
First of all, is this homework?

Quote:
Originally Posted by LQ rules
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
That said, the most direct way to do this is simply to source the file into your script, assuming the values are stored as shell-legal "variable=value" pairs.

Code:
. data.txt

echo "$name"
echo "$password"

I hope you realize that having/storing sensitive password information directly in a script or readable text file is a bit of a security risk. However, some systems, such as samba share mounting, already have a "credentials" option that operates pretty much the same way, since it's safer than including them directly in globally available files like fstab.

http://anothersysadmin.wordpress.com...edential-file/
 
  


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
mplayer fetch subtitles option: --fetch-subs frenchn00b Linux - General 1 11-10-2009 12:21 AM
Crontab + Auto-fetch FTP file yngjzy Linux - General 2 01-03-2009 02:25 PM
Fetch x lines from file G00fy Programming 17 07-18-2008 02:13 PM
How to fetch data from a webpage and a proprietary IOS Mustafa^Qasim Programming 9 04-02-2008 09:50 AM
File Fetch service like Europe Online dr_doom Linux - Software 0 04-06-2004 04:28 PM


All times are GMT -5. The time now is 10:39 PM.

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