Reading variables from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading variables from a file
# 1  
Old 10-04-2011
Reading variables from a file

Hi,

I have an issue that hope someone will be able to help me with....

I'm using a while-read loop to read lines from a file that contain variables, and I want the variables substituted, but I can't get it to work - the below example with explain:
Code:
while read line
   do
      echo $line
      ls -l $line
done < testdata

testdata just contains "$HOME".

I want it to output:
Code:
"/export/home/aperp5" - this is what $HOME is set to
and then list the contents of $HOME

But it outputs:
Code:
$HOME
$HOME: No such file or directory

How can I get it to resolve the variable.

Regards,
Andy

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 10-04-2011 at 01:58 PM.. Reason: code tags
# 2  
Old 10-04-2011
Well normally you input or output (to) a file, meaning either read/write to it, not interpret its content... why put $HOME in a file rather than straight ahead
Code:
 <$HOME

? (because of the garbage .(dot) stuff?) or not use
Code:
pwd>testdata

before to use testdata
# 3  
Old 10-04-2011
That was just a simple example, in reality we will have a file that contains lots of files, and actions that we want to do with them. All the file names will begin with variables like $ORACLE_HOME etc.. i.e:

DELETE:$ORACLE_HOME/bin/test.log

We will then then cut out the action and apply it to the file - there could be a long list of files.

We just need it to substitute the variable value, rather than keeping the variable itself.

Regards,
Andy
# 4  
Old 10-04-2011
add 'eval'
Code:
eval ls -l $line

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 10-04-2011
You will have more responses if you display a sample of the input and desired output.
# 6  
Old 10-04-2011
Quote:
Originally Posted by vgersh99
add 'eval'
Code:
eval ls -l $line

Thanks vgersh99, that worked a treat!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading multiple variables in a loop

Hi, I managed to read and print variable as shown in the below code. table_name=table1,table2,table3 i=0 IFS="," for i in $table_name do echo $i done Is there a way how I can read more than one variable. For example I need to read 2 variables and populate the output... (6 Replies)
Discussion started by: shash
6 Replies

2. Shell Programming and Scripting

Reading control characters into variables

Hi, I am trying to write a shell script to help with some digital signature work currently being undertaken where we have a file that contains a number of rows ending with ^M. What I need to do is concatenate this using shell scripting and retain the control character. E.G. abc^M... (5 Replies)
Discussion started by: chris01010
5 Replies

3. Shell Programming and Scripting

Error reading two input variables

Hello all, I've been out of programming for awhile so sorry about the stupid, elementary question. I'm trying to read two inputs and compare them to a list entered as a parameter via the terminal. The script is #!/bin/bash read -p "Enter the numbers" NUM1 NUM2 for VALUE in $@; do ... (6 Replies)
Discussion started by: EnduranceMan
6 Replies

4. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

5. Shell Programming and Scripting

Help with reading and assigning variables

Hi Gurus, I have a file named log with 2 lines Each line is a file name. eg $ cat log monday tuesday I need to read log and assign each output(filename) to a different variable. The following doesn't work:- while read A B do echo " a is ${A} " echo " b is ${B} " done <... (6 Replies)
Discussion started by: wisdom
6 Replies

6. Shell Programming and Scripting

Reading Awk lines into variables help plz

I have output from luxadm display commands as below :- DEVICE PROPERTIES for disk: /dev/rdsk/c10t60020F200000C8083F951F4C00012863d0s2 Vendor: SUN Product ID: T300 Revision: 0201 Serial Num: Unsupported Unformatted capacity:... (0 Replies)
Discussion started by: lavascript
0 Replies

7. Shell Programming and Scripting

Reading variables from CSV file

Hi I am using KSH and trying to read variables from a csv file. I've set the IFS=, and it workds. Problem is where one of the values is text containing a comma. For example the following lines exist in my file. How can I read everything between the quotes into a single variable? APW13812,,1... (2 Replies)
Discussion started by: ventris
2 Replies

8. Shell Programming and Scripting

reading external variables does not work

... declare vINIFILE vINIFILE=$1 ... echo "The name of the File is $vINIFILE" >>mail_tmp echo "" >> mail_tmp.$$ ... grep RUNJOB=0 $vINIFILE >>tmp_filter ... So the strange is in echo-statement I get the correct output for $vINIFILE wrtitten into the file mail_tmp. But the... (2 Replies)
Discussion started by: ABE2202
2 Replies

9. Shell Programming and Scripting

reading from a file and pass as variables and ignore # in the file

file.txt contains ------------------ sat1 1300 #sat2 2400 sat3 sat4 500 sat5 I need to write a shell script that will output like the below #output sat1.ksh 1300 sat3.ksh sat4.ksh 500 sat5.ksh my try ------- (4 Replies)
Discussion started by: konark
4 Replies

10. Shell Programming and Scripting

reading variables from a file

Hi, Could anyone help me in understanding what I am missing.. I have a text file with the following info. INFILE=> #Name Variable=<value> #--------------------------------- name1 inargs="-a Filename1.$VAR.csv -f Filename2.$VAR.csv -c File.c" name1 ... (4 Replies)
Discussion started by: ttshell
4 Replies
Login or Register to Ask a Question

Featured Tech Videos