LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-25-2004, 08:41 AM   #1
newbielinux
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Rep: Reputation: 0
read from file


Hi all

I am wondering how can I read from file and save the values from
file in some variable using script????

-Need help to do that.....
 
Old 02-25-2004, 08:43 AM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
hmm
VARIABLE = `cat file` ?
 
Old 02-25-2004, 08:52 AM   #3
newbielinux
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by frieza
hmm
VARIABLE = `cat file` ?

Thank you for your reply...
I think I was not clear in my question....I can see my file saying

cat filename

but I am looking to have a script that opens file and read contents of file
while saving certain values in variable....
 
Old 02-25-2004, 08:58 AM   #4
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
hmm, such as?

i'd look into grep and if
 
Old 02-25-2004, 09:04 AM   #5
newbielinux
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by frieza
hmm, such as?

i'd look into grep and if
--------->>>>>>>>>>>>>>>>>>>>>>>>>
I will try to put my questions in more clear words...........

I am not sure how can I read a file using script and store certain values
from that file to varibles in script..

I am new to this so pls assist if you can.
 
Old 02-25-2004, 09:13 AM   #6
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
ok, the values being? words? numbers?
 
Old 02-25-2004, 09:19 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here is an example using read to get the information. It uses a space as a field separator by default.
In this case, the names.txt has information which looks like this...

fred 500
sue 600
tim 500
joe 500

while read USER GROUP reads user then group from each line.

NEW_USERS="/home/names.txt"
cat ${NEW_USERS} | \
while read USER GROUP
do
adduser ${USER} -g ${GROUP}
 
Old 02-25-2004, 09:44 AM   #8
newbielinux
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by homey
Here is an example using read to get the information. It uses a space as a field separator by default.
In this case, the names.txt has information which looks like this...

fred 500
sue 600
tim 500
joe 500

while read USER GROUP reads user then group from each line.

NEW_USERS="/home/names.txt"
cat ${NEW_USERS} | \
while read USER GROUP
do
adduser ${USER} -g ${GROUP}

Thank you very much for your reply.......
although it seems to me it works but I have certain issues related to it....
my file from where I am reading looks like this...
(configuration.test)/////////////////
release test_0.1

patch patch1
//////////////////////////

and my script which is reading this file is:
///////////////////////////////////////////////////////////////////
read_file="/usr/configuration.test"
cat ${read_file}|\
while read USER GROUP
do
adduser ${USER} -g ${GROUP}
done
//////////////////////////////////////////////////

and my output is:
///////////////////////////////////////////////
adduser: unknown group test_0.1
adduser: option requires an argument -- g
usage: adduser [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] name
adduser -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
adduser: unknown group patch1
adduser: option requires an argument -- g
usage: adduser [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] name
adduser -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
adduser: option requires an argument -- g
usage: adduser [-u uid [-o]] [-g group] [-G group,...]
[-d home] [-s shell] [-c comment] [-m [-k template]]
[-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] name
adduser -D [-g group] [-b base] [-s shell]
[-f inactive] [-e expire ]
/////////////////////////////////////

I am not sure why I am getting all that options too...........??????????
 
Old 02-25-2004, 10:11 AM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
adduser: unknown group test_0.1
It starts reading at the top of the file so you can't have anything other than the list you want to read from. In this case, users and groups.
Also, you need to be root to add users.
 
Old 02-25-2004, 10:24 AM   #10
newbielinux
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by homey
It starts reading at the top of the file so you can't have anything other than the list you want to read from. In this case, users and groups.
Also, you need to be root to add users.
--------------------->>>>>>>>>>

Thank you...actually my requirement is bit different...after
reading the file I want to capture certain values leaving certain
lines....ok I should try it with example

File to be read:
////////////////////////////////
release test_0.1

patch
patch1
///////////////////////////////(EOF)
Now I can read all these lines but I need to capture certain
parameters like test_0.1 from first row and patch1 line...

in other words I want to store test_0.1 and patch1 in some variable
...not sure how to achieve it....Thank you for all of your help.....
 
Old 02-25-2004, 11:11 AM   #11
newbielinux
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by newbielinux
--------------------->>>>>>>>>>

Thank you...actually my requirement is bit different...after
reading the file I want to capture certain values leaving certain
lines....ok I should try it with example

File to be read:
////////////////////////////////
release test_0.1

patch
patch1
///////////////////////////////(EOF)
Now I can read all these lines but I need to capture certain
parameters like test_0.1 from first row and patch1 line...

in other words I want to store test_0.1 and patch1 in some variable
...not sure how to achieve it....Thank you for all of your help.....


------------------>>>>>>>>>>>>>>>>>>
I am using this code to read file configuration.test and then trying to use
$release value in sed command.....but if I echo $release outside done loop
I am getting null.....how can I use that value in sed statement..
pls help????????????


read_file="/usr/configuration.test"
cat ${read_file}|\
while read release patch
do
#adduser ${release} -g ${patch}
echo $release
echo $patch
done

#sed "s/test_0.1/`echo $release`/" oldspec.test >spec.test
 
Old 02-25-2004, 01:08 PM   #12
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Ok, forget the adduser part as that is not related to what you are doing.
Maybe this is more what you have in mind....

Code:
#!/bin/bash

cat /usr/configuration.test | grep '[0-9]$'| \
sed -e 's/^\(.*\) \(.*\)/\2/' >file.txt

read_file="file.txt"
cat ${read_file}|\
while read word
do
echo " $word"

done
 
  


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
Read parameters from config file (file parser?) alaios Programming 8 07-09-2012 11:29 AM
How to read .chm file in fedora, can't mount ntfs file system ishti_du Linux - Newbie 12 03-06-2007 03:27 AM
How to read variable from one file & update its value in another file minil Programming 1 03-22-2005 12:12 AM
how do I make a read-only file NOT read-only? robster Linux - General 1 02-17-2004 09:11 PM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:15 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