Problem with parsing filenames containing spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with parsing filenames containing spaces
# 1  
Old 01-24-2009
Problem with parsing filenames containing spaces

I tried using the following options to parse the *.sh files in a dir
(the name can contain spaces). But each of them breaks:

Code:
FILESSH=$(ls /mysh/*.sh)
 
echo "$FILESSH" | while read FILE ; do  --- do something --; done

This does not break for any whitespaces in filenames

Code:
 for FILE in $(echo $FILESSH) ; do  --- do something --; done

This breaks for any space in the name

Code:
for FILE in /mysh/*.sh ; do  --- do something --; done

Code:
echo "$FILESSH" | while IFS= read -r FILE ; do  --- do something --; done


Code 1,3,4 breaks if no sh file is found in the directory (they goes in loop once)....Code 2 does not but it breaks for any space in filename Any better ideas..??
# 2  
Old 01-24-2009
Quote:
Originally Posted by amicon007
Code:
for FILE in /mysh/*.sh ; do  --- do something --; done

...
Code 1,3,4 breaks if no sh file is found in the directory (they goes
in loop once)....Code 2 does not but it breaks for any space in
filename Any better ideas..??

Code:
for FILE in /mysh/*.sh ; do
  [ -f "$FILE" ] || break
  :--- do something --
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

2. Shell Programming and Scripting

Parsing FileNames

Hi, Its been a long time since I've done any shell scripting and I need some help here. Thanks in advance... I need this as a bourne or csh script running under SCO. In a folder I have a list of Backup files named with "TarBackup plus a date and time component suffix" like this; ... (2 Replies)
Discussion started by: stanlyn
2 Replies

3. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

4. Shell Programming and Scripting

Looping through filenames with spaces

I need to loop through the files in a directory and process the files. But some of the filenames contain spaces. Here is a little test script I've been using to experiment. (I'm not really going to call 'echo', I'm doing some other processing.) Everything I try fails. How can I do this??... (7 Replies)
Discussion started by: KenJackson
7 Replies

5. UNIX for Dummies Questions & Answers

parsing filenames

How can I loose a part of the filename I want to drop the “_<Number>.sql” Below I have a listing of file names in a file Eg : CREDIT_DEL_033333.sql I want it to be CREDIT_DEL ATM_DEBIT_CARD_0999999.sql I want it to be ... (3 Replies)
Discussion started by: jville
3 Replies

6. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

7. Shell Programming and Scripting

problem with spaces and argument parsing

public class HelloWorld { public static void main(String args) { System.out.println("Welcome, master"); } } and I compiled using javac HelloWorld.java ] Suppose that I execute the following command directly from the shell: java -XX:OnError="gdb - %p" HelloWorld Then it works... (8 Replies)
Discussion started by: fabulous2
8 Replies

8. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

9. Shell Programming and Scripting

how to handle spaces in filenames

I'm trying to do something like that: for $filename in `ls -1` do some_command $filename done but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around? (4 Replies)
Discussion started by: rayne
4 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question