Unix Kornshell q: Wildcard directory listing limits to first file found

You know how in Unix you can do this:
**
/sales4/dpscalc/prod/log :(beast) >ls -tr LCL_To_DIF_.log >testlist
**
and it will give you a chronologically sorted list of all files matching LCL_To_DIF_
.log that are in the current directory. In my particular environment, the resulting file testlist looks like this:
**
/sales4/dpscalc/prod/log :(beast) >cat testlist
LCL_To_DIF_Cust_Ld.log
LCL_To_DIF_Dlr_Ld.log
LCL_To_DIF_Smrtcrd_Ld.log
LCL_To_DIF_Srvcs_Mstr_Ld.log
LCL_To_DIF_Dlr_Sub_Ld.log
LCL_To_DIF_Sub_Srvcs_Spl.log
LCL_To_DIF_Sub_Hist_Ld.log
LCL_To_DIF_SQLLdr_Sub_Srvcs.log
**

But when I try to use this logic in a Kornshell script, and pass the value LCL_To_DIF_*.log as a paramter, only the first entry in the list gets written to the file, resulting in a file that looks like this:

**
/sales4/dpscalc/prod/log :(beast) >cat testlist
LCL_To_DIF_Cust_Ld.log
**

Why is this happening in this context? How can I work around it? All I’m trying to accomplish is to have a convenient way of selecting a group of log files through a wildcarded spec and then page through them on my screen in chronological order.

What does your script look like? Are you quoting the parameter that you pass? If not, the shell will expand the wildcard before giving it to your script.

Right now I have the following: The part that was supposed to actually display each file is commented out, now that I’ve narrowed down the problem to the way the parameter is being handled. If I use no parameter and hardcode a wildcarded filespec where I now have “$1”, then it works.

**
Jobname=“showlogs”
FileName=""

#rm FileList
ls -tr $1 >FileList
cat FileList
#exec 9<FileList
#while read -u9 i
#do

echo “File name is: $i”

echo “Currently processing $i”

echo “”

more ${i}

echo “Status is $?”

#done
**

OK, So what does your command line look like when calling the script? Do you put the parameter in quotes, like this?



yourscript.sh 'LCL_To_DIF_*.log'


or like this?



yourscript.sh LCL_To_DIF_*.log


If the latter, then the shell is going to expand LCL_To_DIF_*.log into a file list and pass that to your script. Then $1 will simply contain the first file in the list, and pass that to ls. To prove it, add this to the top of your script:



echo "Parameter passed is: $1" 


Make sure you’re quoting the argument right to prevent interpolation. Usually that’s single-quotes, but I’m not too familiar with Korn so it might be different.

Like friedo said, the wildcard expansion is done before the arguments are passed in, so 1 just gets the first file in the list. **friedo** gives one way of solving the problem. Another way would be to use the shell variable * (or $@), which just lists all of the arguments:


ls -tr "$@" > FileList

Thanks for the help.

Both friedo’s and Omphaloskeptic’s approaches work equally well, but I’m going with the latter as it permits me to use the invocation syntax that is more habitual to me. I didn’t know that “$@” was a stand-in for the entire parameter list.