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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Foreach w/ wildcards

Status
Not open for further replies.

komark

Technical User
Sep 12, 2005
134
US
Hi,

I have a text file w/ wildcards

file1
abcd*fg
xy*

I am trying to use foreach to read through the file but not successful because of the wildcards. I am doing:

>foreach x (`cat file1`)
>echo $x
>end

I get echo: No Match

I even used '\' before the asterisk but no luck. Can someone help?
Thanks,
Omar.
 
use double quotes around your var to keep echo from interpretting an asterisk.

[jeff@lab01 ~/tmp]$ foreach x ( `cat file` )
foreach? echo "$x"
foreach? end
file1
abcd*fg
xy*
 
Thanks for your reply.

If i use the double quotes then it doesn't know what to do w/ the asterisk - like if i'm giving part of a file:

foreach x ( `cat file` )
foreach? ls -d /home/test/"$x"
foreach? end

ls: /home/test/abcd*fg: No such file or directory
 
ok, so dont quote your var when you want the asterisk interpreted literally?

what exactly are you trying to accomplish? if you just want to dump a file of wildcards/filenames and pass to ls then no need to quote, if you want to also echo the var then quote when using echo.

 
I was trying to do this in a script.

Change permissions and move to a different directory. Now I have made a list of the actual file list into a different file and ran the foreach command.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top