Using ps2pdf correctly in a script?

I have a script below and I am having problems. I have several folders with PostScript file in them I want to batch convert to PDFs. I have done very little scripting in linux before but I think this is close. It is not quite working how I want it though. Any suggesting? or notice a mistake I made? I want the files to stay in the same location after they have been converted. Currently this converts the files but they get all put together in one folder.

I call the script like this: ./all.ps.to.pdf "/directory/to/process"

#!/bin/sh

STARTDIR=$1
if [ ! -d "$STARTDIR" ]; then
echo No starting directory $STARTDIR
exit 1
fi

find $STARTDIR -name '*.ps' -print | sed -e 's/.ps$//' |
xargs -l -i  ps2pdf \{}.ps

Answers


notice a mistake I made?

Yessss, Sir.

Your find command should be all in one line. Or, if you want to spread it over two lines, use \ as the line continuation sign (which must be the very last character on the first line without any blanks following it!).

That is...

  • either use

    find $STARTDIR -name '*.ps' -print | sed -e 's/.ps$//' | xargs -l -i  ps2pdf \{}.ps
    
  • or use

    find $STARTDIR -name '*.ps' -print | sed -e 's/.ps$//' | \
    xargs -l -i  ps2pdf \{}.ps
    
  • or even use

    find $STARTDIR -name '*.ps' -print \
      | sed -e 's/.ps$//'              \
      | xargs -l -i  ps2pdf \{}.ps
    

(whichever you think looks "nicer" to your Bash reading eyes...).


Just give it the PDF output filename explicitly:

find $STARTDIR -name '*.ps' -print | sed -e 's/.ps$//' | xargs -l -i  ps2pdf '{}.ps' '{}.pdf'

Here is one solution:

find $STARTDIR -name '*.ps' -print |
while read filename
do
  ps2pdf ${filename} ${filename%.ps}.pdf
done

Need Your Help

Slim PHP Framework causing 'Fail to open page'

php api safari slim

I have set up a REST API using the Slim PHP framework, which works perfectly when using requests i.e http://mysite.com/class/id.

render the view using razorengine mvc

html asp.net-mvc razor content-management-system razorengine

I am using RazorEngine to get the view and load it as html.but however my problem is I am reading html code from my database,and show it on my web page .but it gave me string result on my web page ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.