The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
retrive lines from a file using AWK command swamymns Shell Programming and Scripting 1 05-04-2008 06:47 PM
to retrive data that appear only once in a file. anibu Shell Programming and Scripting 1 10-26-2007 07:54 AM
Retrive deleted file's info asmita UNIX for Advanced & Expert Users 4 03-26-2007 10:36 PM
help to retrive data from log file d_swapneel14 Shell Programming and Scripting 5 07-06-2006 01:57 AM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 01:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: Mar 2007
Posts: 30
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Post retrive value from a file

hi
i am new to shell scripting

i have a properties file like
hs=abc
hs1=def
hs2=ghi

now i want to retrive each value and assign it to a variable
like var1 = abc

please help
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
If the file's syntax is compatible with the shell's, you can simply use the source command, the lone dot:

Code:
. file.cf
With the example you have provided, this would assign "abc" to the variable hs, "def" to hs1, and "ghi" to hs2.

If that's not what you want, you could perhaps read in one line at a time, remove everything up through the first equals sign, and then assign the result to some variable.

Code:
for var in var1 var2 var3
do
    read line
    eval $var=\""${line#*=}"\"
done <file.cf
This reads three lines and assigns anything after the first equals sign on each of those lines to the selected variable name; successively, var1, var2, and var3.

The use of eval is a rather advanced technique; I would advise against this if you can come up with a simpler solution.
Reply With Quote
  #3 (permalink)  
Old 05-17-2008
agn agn is offline
Registered User
 

Join Date: Feb 2008
Posts: 76
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
You mean this ?

Code:
for val in $(cut -f 2 -d'=' buf); do var1=$val; echo $var1; done
Reply With Quote
  #4 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: Mar 2007
Posts: 30
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Post retrieve a value fron a file

hi era
i am able to retrieve a value from a file, but i dont know how to assign the retrieved value to a variable. the code i have is
#regressiontest.cfg
hs=abc
hs1=def
hs2=ghi
now i am retrieving the value from the above file
RetrieveCfgvalue()
{
CFG_VALUE=`grep "$2=" $CONFIG_FILE_NAME | cut -d"=" -f2`
CFG_VALUE=`echo $CFG_VALUE|sed -s "s/ *//g"`
CFG_VALUE=`echo $CFG_VALUE|sed -s "s/\"//g"`
if [ ! -z $CFG_VALUE ]
then
eval "$1=$CFG_VALUE"
fi
}

CONFIG_FILE_NAME="regressiontest.cfg"
RetrieveCfgvalue hs hs

now i want to assign the retrieved value(hs) to a variable like
var1 = abc
please help quickly
Reply With Quote
  #5 (permalink)  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Your function looks like it already does this. After RetrieveCfgvalue hs hs you should have the value of hs in the variable hs, isn't that correct? Then if you also want to assign it to var1, you can say var1=$hs (or simply RetrieveCfgvalue var1 hs directly, if that was what you were planning anyway).
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 09:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102