For loop using input file doesn't expand variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop using input file doesn't expand variables
# 1  
Old 05-28-2010
For loop using input file doesn't expand variables

Hi,

I'm using a for loop reading from an input file that contains files, whose path includes a variable name.

But the for loop doesn't expand the variable and therefore can't find the file.

Here's an example:

File BACKUPFILES
Code:
/home/John/alpha
/home/Sue/beta
$BACKUP/listing/gamma

Code:
for i in `cat BACKUPFILES`
do
  file $i
done

Output
Code:
/home/John/alpha:   ascii text

/home/Sue/beta:   ascii text
$BACKUP/listing/gamma:       cannot open: No such file or directory

The variable is set correctly - if I run echo $BACKUP in the for loop, it displays it correctly.

Does anyone know how to get around this? Thanks

Last edited by Franklin52; 05-28-2010 at 09:17 AM.. Reason: Please use code tags!
# 2  
Old 05-28-2010
could you mention where u set the variable value (BACKUP)???
use tag..( in tool bar use # symbol )

thanks
# 3  
Old 05-28-2010
BACKUP is a variable set globally at login - it's not in /etc/profile, but all users process a login script that sets this and other variables.
# 4  
Old 05-28-2010
try this:
Code:
for i in `cat BACKUPFILES`
do
  file $(eval echo $i)
done




another, more efficient approach may be:
Code:
while read i
do
 file $(eval echo $i)
done < BACKUPFILES

# 5  
Old 05-28-2010
Great - both solution worked. Thanks for your help - but could you tell me why the second solution is more efficient than the first? I've got into the habit of using for loops.
# 6  
Old 05-28-2010
Nothing but this avoids creating an extra process with calling an external command (cat).

"While read" the thing for these kind of purpose. ( reading file ).
# 7  
Old 05-28-2010
OK - will remember that. Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Expand Variables and Wildcards into another variable.

Dear Forum members, I am having trouble getting the complete filename (and directory path) in a variable. Output directory mentioned in the code have three files: DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_london.out DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_paris.out... (4 Replies)
Discussion started by: chetanojha
4 Replies

2. Shell Programming and Scripting

For in loop - two input variables

When I create a newfile, I am using the filename as a variable to create the new filename. When I ouput it, the filename contains the file extension in the middle of the file example: router1.txtshcdpneighbors.txt router2.logshcdpneighbors.txt My initial approach was to strip it out, now I... (2 Replies)
Discussion started by: dis0wned
2 Replies

3. Shell Programming and Scripting

HELP - loop a curl command with different variables from input file

Hi guys! Kind of new to bash scripting and now I'm stuck. I need to curl with these variables: "{ \"nodename\": \"$1\", \"ipaddress\": \"$2\", \"poolname\": \"$3\", \"port\": \"$4\", \"loadbalancer\" : \"$5\" }" and my input_file.txt contains server001 10.10.10.01 serverpool1 80... (4 Replies)
Discussion started by: yort
4 Replies

4. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. Shell Programming and Scripting

Passing variables to an input file

Hi All, I have to insert 2 values to a text file in specific places. I have been able to extract each variable value via a script but am not able to send these variable values to the text file. Pasted is the script for extracting the variable values: for i in `ls -1` ... (2 Replies)
Discussion started by: danish0909
2 Replies

6. Shell Programming and Scripting

To take two variables from a file and act as an input for the script

Hi, I have done the scripting such that it will read input line by line from a txt file and is passed through a script, but now my requirement is to pass two variables into a script from a file, how could I do this or is there any other better idea ? for reading singe input from a file, line... (4 Replies)
Discussion started by: ajothi
4 Replies

7. Shell Programming and Scripting

make loop from input file

Hi Guys, I have file A.txt PP97 PP66 PP87 PP66 PP47 PP57 PP44 PP20 PP66 PP16 PP13 PP51 PP68 PP70 PP75 PP30 (2 Replies)
Discussion started by: asavaliya
2 Replies

8. Shell Programming and Scripting

dynamic variables - eval - expand etc.

Hello, so i'm making a script, using dynamic variables and trying to expand them. So far it hasn't worked out too well so it seems that I need some help from you, the elite. Example: #!/bin/sh counter=0 until (($counter>5)) counter2=1 until (($counter2>6)); do if ;... (5 Replies)
Discussion started by: TehOne
5 Replies

9. UNIX for Dummies Questions & Answers

how to expand environment variables in a file?

I am new to unix and would appreciate if someone could help. I have an environment variable SourceFilePath=/db1/Src/test set on the unix server. I want to expand this SHELL variable in a file using any command sed, awk etc File contents is as follows: var=$SourceFilePath/file.txt ... (2 Replies)
Discussion started by: debbie15
2 Replies

10. Shell Programming and Scripting

need help with using input file in the while loop

here's my code: ls -lt | /usr/bin/egrep `date +"%Y%m%d(05|06|07|08|09|10|11|12|13|14|15|16|17|18|19)"` | \ /usr/bin/egrep -i .dat | /usr/bin/awk '{print $9}' > $LockboxFile1 if ] then ... (2 Replies)
Discussion started by: sytycd712
2 Replies
Login or Register to Ask a Question