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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-13-2003, 08:38 AM
|
#1
|
|
Member
Registered: Apr 2003
Distribution: Red Hat
Posts: 65
Rep:
|
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...)
|
|
|
|
08-13-2003, 08:51 AM
|
#2
|
|
Senior Member
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251
Rep:
|
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
|
|
|
|
08-13-2003, 09:17 AM
|
#3
|
|
Member
Registered: Apr 2003
Distribution: Red Hat
Posts: 65
Original Poster
Rep:
|
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
|
|
|
|
08-13-2003, 09:28 AM
|
#4
|
|
Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu
Posts: 12,611
Rep:
|
Moving to Programming
Cool
|
|
|
|
08-13-2003, 01:26 PM
|
#5
|
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
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="
"
|
|
|
|
08-15-2003, 04:33 AM
|
#6
|
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:14 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|