LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 09: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, 09: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, 10: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, 10:28 AM   #4
MasterC
Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu
Posts: 12,612

Rep: Reputation: 64
Moving to Programming



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

Rep: Reputation: 65
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, 05:33 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:51 PM
bash scripting problem sutley Linux - Software 1 12-17-2004 12:33 PM
bash scripting problem Sammy2ooo Linux - General 3 12-13-2004 01:31 PM
BASH Scripting Design Problem... amaze Linux - General 1 08-19-2003 09:30 AM
bash scripting problem raven Programming 7 03-10-2003 06:48 PM


All times are GMT -5. The time now is 07:17 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration