LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash for loop problem (https://www.linuxquestions.org/questions/programming-9/bash-for-loop-problem-88869/)

deadlock 09-03-2003 12:10 PM

bash for loop problem
 
After some earlier help in this forum I have created a bash script that watches for files in a folder "in" and outputs pdf files to an "out" folder. During testing however, I have found that there is a problem when the loop is blank.

The basic code is as follows:

cd in
for file in *
do
// create pdf for $file
done

However when the folder "in" is empty, linux insists on passing a file "*" back to the script. This that messes up the logging of events and the pdf distiller.

I have tried putting a line such as:

if $file <> "*"

but bash gives an error at the line

Any suggestions?

(the for file in * was the best solution for the earlier problems)

Cheers

Dan

david_ross 09-03-2003 12:46 PM

Try using ls:
Code:

for file in `ls /path/to/in/*`
do
// create pdf for $file
done


Strike 09-03-2003 02:19 PM

Using make might be a better idea for this, just as an aside.

deadlock 09-04-2003 02:47 AM

Thanks

I have tried using

for file in ls...

and had problems with spaces and command chars in file names being interpreted by bash - unfortunately I need to be able to treat the names exactly as they are.

How do you mean use make???

Dan

unSpawn 09-04-2003 04:08 AM

If you execute your script in debug mode (sh -x <script>) you'd see the "for" loop breaking the results from "ls" up. If you use a "while" loop it won't. Only listing files with for instance the .ps extension limits the output of "ls", basename strips the extension which comes in handy when moving if your create_pdf_cmd doesn't output to stdout and doesn't have output options.

Code:

ls in/*.ps|while read file; do
filen=$(basename "$file" .ps)
create_pdf_cmd "$file" > in/"$filen".pdf
mv in/"$filen".pdf out; done

...or with "xargs" if you don't want to use a "while" loop. You're not restricted to using "ls" as well. If you know your input filetype always is Postscript and has a postscript extension, you could use
Code:

find in -type f -name \*.ps|xargs -ix ps2pdf "x"
else skip the "-name \*.ps" part. Note I don't use curly braces, I use "-ix" to get quoted variable x.

deadlock 09-04-2003 04:32 AM

That looks great, one quick question. The source files will either be .ps or .prn, which ghostscript copes with equally well, so the ls command I am using is:

Code:

ls $dir/in/{*.ps,*.prn}|while....
However if the "in" directory is empty, or there are no .ps files (and are .prn files) I get an on-screen error such as:

Code:

ls: .../in/*.ps: No such file or directory
As I want this script to be run on a cron, I obviously don't want to have messages filling up the log. As this error is obviously useless (as I don't need to know the folders empty) would a redirect to /dev/null be OK? e.g.

Code:

ls $dir/in/{*.ps,*.prn} 2>/dev/null|while....
or do I need to redirect the results to the while loop?

Also, can I get the basename command to cope with the 2 different extensions?

Dan


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