Paul Varghese
Paul Varghese

Reputation: 1655

for loop not working as expected with variable in ksh

The following piece of code is printing /home/user/dir/*@(${host}|${hostname})* instead of printing each of the files in the directory.

#!/usr/bin/ksh

host=foo
hostname=bar 
config_path="/home/user/dir"

search=$config_path/*_@(${host}|${hostname})_*

for file in $search
do                    
  echo $file
done

And It works if I do like this

#!/usr/bin/ksh

host=foo
hostname=bar 
config_path="/home/user/dir"

#search=$config_path/*_@(${host}|${hostname})_*

for file in $config_path/*_@(${host}|${hostname})_*
do                    
  echo $file
done

I've three questions.

1) Why its returning the string when pattern is assigned to variable?

2) I'm using this pattern in so many places, so it's better to assign it a variable. How to fix this?

3) Is it fixed in the newer version of ksh?

I'm using SunOS server 5.10 Generic_147441-23 i86pc i386 i86pc and got ksh version by typing the following command.

$ set -o vi
$ Version M-11/16/88i

Upvotes: 1

Views: 440

Answers (1)

Guru
Guru

Reputation: 16974

To fix this, you can do:

for file in $(eval echo $search)

Upvotes: 3

Related Questions