|
Avoiding Split of a File Name Containing Blanks
Hey, Ted:
I'm new to Qshell script programming and enjoy your
articles about the subject very much. So much, that I've
started using Qshell when operating on stream files in the
OS/400 IFS. I have encountered one problem though: files
with one or more blanks in their name.
Here is an example:
files=$( ls *.csv )
for file in $files
do
echo $file
done
The problem is in the for loop. When a file name
contains blank(s), the variable file is first set with the
first portion of the file name up to the first blank and the
control block is done, then file is set to the next portion
of the same file name.
I want to retrieve the whole name of the file,
including blanks. How do I do that?
-- Christian
You've run into a powerful, but tricky, feature of the
Qshell--globbing, or the expansion of expressions into lists
of file names.
In your example, Christian, you use globbing in the first
line to build a list of file names in the files variable.
The file names are separated by one space. Therefore, if you
have two csv files in your directory--a file.csv and
cust.csv--the variable files takes this value:
a file.csv cust.csv
The for loop does not know that this is supposed to be a
list of files and sees it as a list of three strings
separated by blanks.
The solution to your problem is to delay globbing until
the for loop executes:
for file in *.csv
do
echo $file
done
-- Ted
|