Reading Variables From another file

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

Reading Variables From another file

Postby mudasir » Sun Nov 04, 2007 9:40 pm

AOA,

I am facing a problem, please help me out.
I want to store variables in a file those variables will users input.
example

/etc/getinfo
echo Please Enter Your First Name
read fname
echo Please Enter Your Last Name
read lname


Now what i want to do is to store these variables in a file present at
"/etc/info" like

FNAME=(input by the person in /etc/getinfo)
LNAME=(input by the person in /etc/getinfo)


And one more thing, how can i call these variables in other files.

Please Help me out....
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby mudasir » Mon Nov 05, 2007 11:01 pm

AOA,

I have solved a bit of my problem by my self, still having one problem...
my test.sh is

Code: Select all

#!/bin/sh

echo Please Enter Your First Name
read fname
echo Please Enter Your Last Name
echo lname
echo FNAME=$fname LNAME=$lname > /files/test/shell/info
echo Your Full Name is $fname $lname


Now the variables are being stored in /files/test/info but in this format
FNAME=Mudasir LNAME=Mirza


Now i want these variables to be stored like

FNAME=Mudasir
LNAME=Mirza


Please Help me out.....
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby mudasir » Tue Nov 06, 2007 4:11 am

AOA,

I figure out the way to that also....

Code: Select all

echo Please Enter Your First Name
read fname
echo Please Enter Your Last Name
read lname
rm -f /files/shell/info
touch /files/shell/info
echo -e "FNAME=$fname \nLNAME=$lname\n" >> /files/shell/info
echo Your Full Name is $fname $lname


Variables are being stored as i want in /files/shell/info

FNAME=Mudasir
LNAME=Mirza


the only thing that i want to know now is how to read these variables from /files/shell/info

like i have another script which uses these variables to welcome users, now i only want to know is that what is the matter to call variables from /files/shell/info

Please Help me out now....i have been searching google for this for almost 3 days.....
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby mudasir » Wed Nov 07, 2007 6:29 pm

AOA,

Please help me out...
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby sameer666 » Thu Nov 08, 2007 3:11 am


#!/bin/bash

echo Please enter your first name:
read fname
echo Please enter your last name:
read lname

touch info.log

echo -e "Fname=$fname\nLname=$lname\n" >> info.log

cat info.log | while read line;

do

echo | awk '

{

print substr ("'"$line"'",7,20)

}'

done




hope it helps
Novice at heart
sameer666
Naik
 
Posts: 82
Joined: Tue Nov 06, 2007 5:31 am

Postby mudasir » Thu Nov 08, 2007 7:47 am

AOA,

Thanks alot sameer666, how ever this is not the type of output i am looking for. What i want to do is to use the variables present in info.log file
FNAME or LNAME or any other variable. I dont want the output to be printed or i dont want any thing to do with the output.

I tried the same thing with the following code

Code: Select all

echo Please Enter Your First Name
read fname
echo Please Enter Your Last Name
read lname
rm -f /files/shell/info
touch /files/shell/info
echo -e "FNAME=$fname \nLNAME=$lname\n" >> /files/shell/info

for var in `cat /files/shell/info`
do
lst=`echo $var | cut -d"=" -f2`
echo $lst
done

This code gives me the same result as your code.

What i want to do is like when ever i write $FNAME in a file it should get its value from /files/shell/info

This is what i want to do.

Hope I conveyed my problem right this time.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby sameer666 » Thu Nov 08, 2007 3:34 pm

hi,


What i want to do is like when ever i write $FNAME in a file it should get its value from /files/shell/info


what you mean by this, and please can you explain your problem again, because it is kinda vague.

thanks
Novice at heart
sameer666
Naik
 
Posts: 82
Joined: Tue Nov 06, 2007 5:31 am

Postby mudasir » Thu Nov 08, 2007 5:28 pm

AOA,

Dear sameer666,

By this i mean, for example in another script /files/shell/getinfo

Code: Select all


#!/bin/sh
echo Welcome $FNAME To Test-PC

like here i dont want to define this FNAME=Mudasir variable, i want that this variable should be called from file /files/shell/info.

Hope you understand my problem.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Re:

Postby LinuxFreaK » Thu Nov 08, 2007 10:42 pm

Dear mudasir,
Salam,

I believe you are trying to learn bash scripting which is good. I just want to give you good Bash FAQ.

FYI, http://wooledge.org/mywiki/BashFAQ

Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Postby sameer666 » Fri Nov 09, 2007 12:44 am

hi

you can export Fname and then use it in ur script.
Novice at heart
sameer666
Naik
 
Posts: 82
Joined: Tue Nov 06, 2007 5:31 am

Postby mudasir » Fri Nov 09, 2007 1:32 am

AOA,

Thanks farrukh bhai this FAQ pag will help me alot...

Dear Sameer666,
can you please guide me how can i EXPORT or IMPORT a variable.
I think calling a variable from another file will be IMPORTING a variable...

Please help me out
Last edited by mudasir on Fri Nov 09, 2007 5:43 am, edited 1 time in total.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby sameer666 » Fri Nov 09, 2007 4:14 am

Code: Select all

#!/bin/bash

echo Please enter your first name:
read fname
echo Please enter your last name:
read lname

touch info.log

echo -e "Fname=$fname\nLname=$lname\n" >> info.log

cat info.log | while read line;

do

echo | awk '

{

print substr ("'"$line"'",7,20)

}'

done

export Fname
/bin/bash


hope it helps
Novice at heart
sameer666
Naik
 
Posts: 82
Joined: Tue Nov 06, 2007 5:31 am

Postby mudasir » Fri Nov 09, 2007 5:42 am

AOA,

Dear sameer666,

This is the same code as you posted before, i think i am unable to convey my precise problem to you.
Let me define my problem again.
there are 2 scripts,

script no 1---which gets users inputs and saves that input in a file (which i have made)

script no 2---now this script will need the information entered by user, now as that information is saved in variables form in the file by script no 1, so is there any way that i will just write the variable and it will get its value from that file.
like in script no 2 i write
echo $FNAME
FNAME is a variable in the file containing users inputs which were saved by script no 1.
so in this way i can use this variable as may times as i want and dont have to define its value, it will fetch its value from the file.


I hope you understand my problem, please help me out.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby sameer666 » Fri Nov 09, 2007 5:06 pm

hi

i got your problem, see the point your missing is that you have to read the file and the data that is read have to be stored in some variable in the memory.

Code: Select all

#!/bin/bash

echo Please enter your first name:
read fname
echo Please enter your last name:
read lname

touch info.log

echo -e "Fname=$fname\nLname=$lname\n" >> info.log

cat info.log | while read line;

do

echo | awk '

{

print substr ("'"$line"'",7,20)

}'

done

export Fname #makes and an environmental variable
/bin/bash        #invoke child bash shell with Fname set




if after running the above script you do, echo $Fname in bash it will print the value of Fname. hope you understand what i mean. beside setting the environmental variable or reading from the file, you just cann't get the info magically. :D

hope it helps
Novice at heart
sameer666
Naik
 
Posts: 82
Joined: Tue Nov 06, 2007 5:31 am

Postby mudasir » Fri Nov 09, 2007 5:34 pm

AOA,

Dear sameer666,

This is code is not working. Any ways thanks alot Sameer bhai for helping me out alot...

And Farrukh Bhai the FAQ page as cleared alot of my questions...THANKS ALOT for that page.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron