LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2003, 08:38 AM   #1
deadlock
Member
 
Registered: Apr 2003
Distribution: Red Hat
Posts: 65

Rep: Reputation: 15
BASH scripting problem


I am attempting to create a script that takes all the files in a directory "in", converts them to pdfs in an "out" directory, and creates a log file of the same name also in the "out" directory.

The script needs to be able to cope with space and control characters in the target file names.

the script I am using at the moment is:

for file in `ls in`
do
echo $file >> "out/$file.log"
date >> "out/$file.log"
# then create pdf files...
done

This creates the log files, but will wrap $file at any spaces.

I have tried using ls -Q, but this creates errors. I have also tried "`ls in`", but this creates a very long file name.

Any suggestions...

Dan
(and restricting file names is not an option...)
 
Old 08-13-2003, 08:51 AM   #2
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
instead of saying

for file in `ls in`

instead say

for file in in/*

the difference is that 'ls in' would provide a list of text strings but the in/* would then be shell substitution so the filenames don't get distorted.

hope that helps.
jpbarto
 
Old 08-13-2003, 09:17 AM   #3
deadlock
Member
 
Registered: Apr 2003
Distribution: Red Hat
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks for that...

The only problem that I can see with that is that it returns the results as being:

in/Site Report 1.prn
in/Site Report 2.prn
...etc

which makes it a bit more difficult to shift things across to the out directory. For the moment I will need to use a couple of "cd" commands in the script... not really a problem, but if there's a better solution it would be appreciated.

Dan
 
Old 08-13-2003, 09:28 AM   #4
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
Moving to Programming



Cool
 
Old 08-13-2003, 01:26 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
You could either seperate the name from the dir using:
file = `echo $file | cut -d"\/" -f2`

Or go back to the ls command and set the field seperator to a newline before the "for" command:
IFS="
"
 
Old 08-15-2003, 04:33 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
If you use find you won't have the leading dir problem in/Site Report 1.prn.

for file in `find in/*`

Also if you use double quotes around "$file" you wont have the broken filename problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting problem (bug?) zz9pluralzalpha Linux - Software 2 04-12-2005 01:51 PM
bash scripting problem sutley Linux - Software 1 12-17-2004 11:33 AM
bash scripting problem Sammy2ooo Linux - General 3 12-13-2004 12:31 PM
BASH Scripting Design Problem... amaze Linux - General 1 08-19-2003 08:30 AM
bash scripting problem raven Programming 7 03-10-2003 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration