Solved

KORN SHELL : Reading file names with spaces

Posted on 1997-07-04
1
291 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 64
Pressing cancel button with C# UI Automation 3 70
modThree challenge 4 91
Sed question 2 86
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…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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