Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested function not recognizing a variable

Status
Not open for further replies.

ryanc2

MIS
Apr 18, 2003
73
US
I'm calling a function that contains a variable defined within a while loop, but it is not being recognized. An example would be:

#!/bin/ksh
list=list_${F}.txt
.
.
.
function () {
if [ -f ${list} ]; then echo ${list} does not exist; fi
}
.
.
.
set `cat other_list.txt`
while :
do
for F
do
function
done
done

The result if you echo $list is list_.txt (The $F is not recognized).

The contents of other_list.txt is a and b, so the loop should look for list_a.txt and then list_b.txt.

What did I screw up?
 
list=list_${F}.txt is evaluated only once at atartup.
In your shell man page take a look at the eval shell buitin.


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I came to that conclusion shortly after and moved the variable definition to within the funciton - voila! Thanks.

However, now I've run into a sed question:

TEST${F}12345.DONE needs to be TEST${F}.DAT

I never can seem to get sed right.

Thanks in advance.
 
ryanc2,

How about this?

echo 'TEST${F}12345.DONE' | sed 's/[0-9]\{5\}.DONE/.DAT/g'

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top