Solved

KORN SHELL : Reading file names with spaces

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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