The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 05-17-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,605
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