LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-12-2008, 09:14 PM   #1
Mikecore
Member
 
Registered: Dec 2003
Distribution: Fedora Core 1
Posts: 35

Rep: Reputation: 15
Newbie Bash script help


I'm new to bash scripting so be kind. I'm trying to write a script to batch process a bunch of pdf files through the tool "pdftk" here is what I have so far.

Code:
#!/bin/bash

find *.pdf | pdftk *.pdf input_pw password output open_*.pdf; if [ "$?" != 0 ]; then *.pdf >> fail.txt }
problem is this doesn't work it loads the files but fails the conversion. and when I run the same commmand on just one file it works fine.

Code:
pdftk file_name.pdf input_pw password output open_file_name.pdf
 
Old 05-12-2008, 09:45 PM   #2
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
Hey there,

I think there might be a couple reasons why you're having issues with the multiple conversions (I'll be kind

This code:

Code:
find *.pdf | pdftk *.pdf input_pw password output open_*.pdf; if [ "$?" != 0 ]; then *.pdf >> fail.txt
would change to this (keeping it relatively the same) and easy to read, versus less efficient

Code:
find . -name "*.pdf" |sed -e 's/\.pdf//' -e 's/^\.\///'| while read x; do pdftk ${x}.pdf input_pw password output open_$x.pdf; if [ "$?" != 0 ]; then echo ${x}.pdf >> fail.txt;fi;done
This more convoluted than it needs to be, but, hopefully walks you through the process better. Take the output from the find command (changed to specify the current directory and quote the found filenames to avoid system globbing (*) reading anything from your current directory), process it with sed to remove the extraneous beginning ./ and ending .pdf from each filename, then pump that to a while loop where you run your pdftk command. In that while loop you run your if-conditional to check and see if it worked, with an ending "fi" to close the if loop and a "done" to close the while loop when you're done processing pdf's

Hope that helps and is easier to understand than it was to explain

, Mike
 
Old 05-12-2008, 11:38 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you're doing a numeric comparison here:

if [ "$?" != 0 ]

that should be

if [[ $? -ne 0 ]]

http://www.tldp.org/LDP/abs/html/tes...ml#DBLBRACKETS
http://www.tldp.org/LDP/abs/html/comparison-ops.html
 
Old 05-13-2008, 11:23 AM   #4
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
True,

Good catch. I missed that.

The curse of knowing too many scripting languages

, Mike
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash Script newbie Konane Programming 9 11-18-2007 12:46 PM
Bash script, read file into array (Newbie) TheMeeks Programming 6 04-17-2007 08:20 AM
Newbie bash question about a secondhand script madtinkerer Programming 22 11-27-2006 08:02 AM

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

All times are GMT -5. The time now is 08:06 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
Open Source Consulting | Domain Registration