ls not working as expected within ksh

Hi,
I use the command

ls a\b\c\*.txt

from the command line on HP UNIX and it works fine - It lists all files matching *.txt in the a\b\c directory

When embeded in a ksh script

`ls a\b\c\*.txt`

it does not work - I get *.txt not found (even though there are files)

I tried variations like having escape / and such but no use.

Please help. Thanks much.

GNMIKE :slight_smile:

For a start, your slashes are the wrong way round, but I'll assume that's a typo :wink:

Anyway, having

`ls /foo/bar/*.txt`

will result in a lot of "not found" errors. This is not the way to use command substitution.

my_variable=`ls /foo/bar/*.txt` will work fine.

In your script, just use
ls /foo/bar/*.txt
and it should work fine.

If you're still having problems, please post your entire script.

Cheers
ZB

Hi,
Thanks for your help - Here is a part of my script...

datapath would have the valye a/b/c
datapatrn would have the value /.txt (I tried /\.txt also)
batchsize would have a number such as 2 for example
the result should go to a file $fileslst

`ls -F1rt "$datapath" "$datafilepatrn"|head -"$batchsize">"$fileslist"`

thanks again for your help

The flaw is if you run inside the directory where a is located it will execute, but if you run script through some other direcotry then it will not be able to find the a/b/c path iteself.

use something like
datapath=$source_dir/a/b/c
datafilepatern='*.txt'

ls -F1rt $datapath/$datafilepattern | head -"$batchsize">"$fileslist"

Hi,
I see your point but the problem is the same. I get *.txt not found!

Change
`ls -F1rt "$datapath" "$datafilepatrn"|head -"$batchsize">"$fileslist"`
to
ls -F1rt "$datapath$datafilepatrn"|head -"$batchsize">"$fileslist"

i.e. remove the backticks, and remove the space.

Can't guarantee anything without seeing the rest of your script, but if my hunch is correct - that'll fix it.

Cheers
ZB

I tried this but it still does not work...

#!/usr/bin/ksh
set -x
datapath=/home/oracle/APPS/mobilydw/na/source/gsm/
datafilepatrn="*.txt"

fileslist=/home/oracle/APPS/mobilydw/na/bin/

ls -F1rt "$datapath""$datafilepatrn"|head -10>"$fileslist"

exit

ls -F1rt "$datapath""$datafilepatrn"
is wrong. Use:
ls -F1rt ${datapath}${datafilepatrn}

Great hdelp.
It worked!

could you please tell me why "..." is wrong and {..} work - or point me to somehwere I could read about the difference?

Thanks a lot.

Metacharacters like * do not get expanded inside double quotes. Type
man ksh
for ksh info.

Excellent help. Thanks pal. :slight_smile: