Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

KORN SHELL : Reading file names with spaces

Posted on 1997-07-04
1
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
dhm earned 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

719 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