Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-02-2012, 05:18 AM   #1
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

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, 05:41 AM   #2
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, 06:53 AM   #3
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, 07:22 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
In very simple terms
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, 03: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: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
First of all, is this homework?

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.

. 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.


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

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

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