Solved

KORN SHELL : Reading file names with spaces

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now