Solved

KORN SHELL : Reading file names with spaces

Posted on 1997-07-04
1
316 Views
Last Modified: 2013-12-26
I need to write a script which sorts through folders and files and performs file management (e.g. rm) on certain files.  I wrote the names to a file in order to read them in one at a time.  Everything works fine until I run across files with spaces in them (which are generated from a Macintosh environment).

How can I read back the file names that contain spaces and do operations on these files without the ksh seeing to file names instead of one name with a space in it?

0
Comment
Question by:randyn
1 Comment
 
LVL 3

Accepted Solution

by:
dhm earned 100 total points
ID: 1293860
Easy enough...just put double quotes around the variable containing the filename when you use it.  Here's a simple ksh script that finds all the files in or below the current directory and executes "ls -l" on each one:

#!/bin/ksh

find . -type f -print | while read FN ; do
      echo "Found a file: $FN"
      ls -l "$FN"
# Note: without the double quotes above, this won't work!
done

----------------------------------------------------

I created a directory containing some files with no spaces in the names, and others with a space (lower-case filenames "a", "b", and "c" have no spaces; upper-case names "A A", "B B", and "C C" do).  When I run this script, I get:

dhm@eagleone[38]$ ./test.ksh
Found a file: ./foo/a
-rw-rw-r--   1 dhm      webvisio        0 Jul  4 10:53 ./foo/a
Found a file: ./foo/b
-rw-rw-r--   1 dhm      webvisio        0 Jul  4 10:53 ./foo/b
Found a file: ./foo/c
-rw-rw-r--   1 dhm      webvisio        0 Jul  4 10:53 ./foo/c
Found a file: ./foo/A A
-rw-rw-r--   1 dhm      webvisio        0 Jul  4 10:53 ./foo/A A
Found a file: ./foo/B B
-rw-rw-r--   1 dhm      webvisio        0 Jul  4 10:53 ./foo/B B
Found a file: ./foo/C C
-rw-rw-r--   1 dhm      webvisio        0 Jul  4 10:53 ./foo/C C
Found a file: ./test.ksh
-rwxr-xr-x   1 dhm      webvisio      100 Jul  4 10:57 ./test.ksh
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 84
Process filename extension 3 207
Detect CR LF to each line 12 174
zeroFront challenge 7 121
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question