shell not converting environment variable values

February 11, 2012 at 11:59:11
Specs: Solaris (Korn Shell)
How can I get the value of the environment variable that is inside a text. I have the script below and assigning the directory value which is a combination of an env variable with some directory but looks like it is not being translated to its actual value

I would like to list the contents of the the directory list in dir_file config file

here is my set up:

environment variable
HOME_DIR=/export/home/user1

dir_file file has the following lines:
$HOME_DIR/some_directory1
$HOME_DIR/some_directory2
and so on...

The script:
#!/usr/bin/ksh
for i in `cat dir_file`
do
echo $i
ls $i
done

output:
$HOME_DIR/some_directory1
$HOME_DIR/some_directory1: No such file or directory
$HOME_DIR/some_directory2
$HOME_DIR/some_directory1: No such file or directory


See More: shell not converting environment variable values

Report •

#1
February 11, 2012 at 12:25:21
Place eval in front of the ls command. eval forces $i to be interpreted as an argument to ls:

while read i
do
   echo $i
   eval ls $i
done < dir_file
# end script

I choose to use a while loop instead of a for loop. Read here to find out why:

http://partmaps.org/era/unix/award....


Report •

#2
February 14, 2012 at 16:42:28
Thank you very much Nails! The eval command got the job done, also learned a lot from the link you provided.

Report •
Related Solutions


Ask Question