LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-30-2009, 10:51 AM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Rep: Reputation: 39
Creating a loop within a script using ps2pdf


Hi there --

I am in the process of writing an interactive script that is designed to go to a directory, and once there convert the postscript, .ps, files into .pdf files.

The ps2pdf command is already in place, but I am trying to come up with a way to have the script go through the directory, and run the command on every .ps file that it sees. What is the best command loop syntax to use here? Thanks.
 
Old 09-30-2009, 11:09 AM   #2
xtothat
Member
 
Registered: Sep 2008
Location: Middle of Nowhere, England
Distribution: Slackware 14.1, Ubuntu 13.10
Posts: 39

Rep: Reputation: 15
Hi there!

It's pretty simple.

If you're wanting to run a simple command inside a loop through files in a folder:

Code:
#!/bin/bash
cd /folder/name/here
for i in *.ps; do
    insert ps2pdf command here, but substitute the filename for $i
done
Job done!

Any problems with this shout me, I've written this in about 30secs before heading out of the office!

X-T
 
Old 09-30-2009, 11:13 AM   #3
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Wink

Hi there --

Thanks for the reply. I'll give it a whirl, and let you know. Suffice it to say, you put me to shame in being able to create this in so little time.
 
Old 09-30-2009, 12:26 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
It depends. That simple loop will work if you have a bunch of ps files in a single directory (remember to use quotation marks though, as in "$i" and not just $i, otherwise a blank space in the middle of a file name and you are screwed).

If you need to recurse into subdirectories then the thing is slightly more complicated, and your best bet would probably be to use "find", as in this example:

Code:
find /path/to -iname \*.ps -print0 | while IFS= read -d '' i; do echo ps2pdf "$i" "${i%.ps}.pdf"; done
Or whatever the ps2pdf syntax is, this is just an example. Of course, remove the "echo" to do the real work.
 
Old 10-01-2009, 10:13 AM   #5
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hi there --

I have made significant process with the script. There is only one sticking point, and I was hoping I could get it resolved with some help.

The script, in its current form is shown below:

Code:
#!/bin/bash

# The purpose of this script is to go to the directory in question,
# and convert all postscript, .ps, files located there to .pdf files.

# Provide a list of the cases directory for the user.
cd /home/rsa/data/xknife_data/cases

echo CONVERSION OF POSTSCRIPT, .ps, FILES TO ADOBE ACROBAT, .pdf, FORMAT.
echo
echo
echo The patients in the cases folder will now be listed for your convenience.
echo
echo It is important that you make note of the exact spelling of the directory
echo in question in order for this process to work.
echo
echo
read -p "Press any key, other than the spacebar in this case, to continue..."

ls -lh | more
echo
echo
echo Please enter the directory that is the object of this exercise.
echo
read $directory
echo Is $directory the patient in question? (y/n)


# Change to the directory in question
cd /home/rsa/data/xknife_data/cases/$directory

# Copy the .ps files to a local directory
cp -pr *.ps /tmp/ps2pdf

# Change to the temporary directory
cd  /tmp/ps2pdf

# List the files that were copied over the temporary directory
ls -l

# Convert the files in the temporary directory to .pdf format.
for i in *.ps; 
do ps2pdf "$i"; 
done

# Copy the .pdf files back to the original location
cp -pr *.pdf /home/rsa/data/xknife_data/cases/$directory

# Remove the files from the temporary directory
rm -rf *.ps
rm -rf *.pdf

echo The conversion of the postscript files to acrobat format is complete.
echo Use an ftp client application to retrieve the files from the patient directory.
The sticking has to do with the user prompt for the directory in question. I need to come up with a for loop that will take the input, and either go to the directory that was entered, or repeat the question that was asked at the beginning. How would I do that? Thanks.
 
Old 10-01-2009, 10:36 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by kaplan71 View Post
I need to come up with a for loop that will take the input, and either go to the directory that was entered, or repeat the question that was asked at the beginning. How would I do that? Thanks.
What are your criteria for deciding whether to go or repeat? I guess non-existent or unreachable directory ... anything else?
Code:
while :
do
    echo -n '<prompt string>: '
    read
    [[ <$REPLY meets criteria> ]] && break
    echo <error message> >&2
done
You might also want to give the user a nice option to quit (reply q or Q?) or inform them that Ctrl+C will do same.
 
Old 10-01-2009, 12:14 PM   #7
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hi there --

I inserted the command syntax that was posted into the script. The modified version of the section of the script in question is shown below:

Code:
ls -lh | more
echo
echo
echo Please enter the directory that is the object of this exercise.
echo
read $directory
read Is $directory the patient in question? 
while :
do
	echo -n 'y: '
	read
	[[ <$REPLY meets criteria> ]] && break
	echo The patient in question was not found. >&2
done
When I ran the script, and inserted the directory name, the script appeared to hang, and no pdf files were generated.

I confirmed, by manually running each line in the script, that the script was in the correct location to search for the directory in question. What am I missing here? Thanks.
 
Old 10-01-2009, 07:06 PM   #8
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by kaplan71 View Post
Hi there --

I inserted the command syntax that was posted into the script. The modified version of the section of the script in question is shown below:

Code:
ls -lh | more
echo
echo
echo Please enter the directory that is the object of this exercise.
echo
read $directory
read Is $directory the patient in question? 
while :
do
	echo -n 'y: '
	read
	[[ <$REPLY meets criteria> ]] && break
	echo The patient in question was not found. >&2
done
When I ran the script, and inserted the directory name, the script appeared to hang, and no pdf files were generated.

I confirmed, by manually running each line in the script, that the script was in the correct location to search for the directory in question. What am I missing here? Thanks.
Is this the same shell script task you started with, e.g. converting .ps files to .pdf? I ask because your script doesn't do that -- not remotely.

Have you tried something like this:

Code:
search_depth=1
file_suffix="\.ps$"

while true
do
   valid_entry=false
   while ! $valid_entry
   do
      echo -n "Enter desired directory or q=quit:"
      read dir
      [ "$dir" == "q" ] && exit 0
      [ -e "$dir" ] && valid_dir=true
   done
   find $dir -maxdepth $search_depth -type f | grep -P $file_suffix | while read filepath
   do
      echo "do something with $filepath here."
   done
done
At the moment the script just prints any filenames it finds -- it's up to you to put the actual command in place.
 
Old 10-02-2009, 05:43 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by kaplan71 View Post
Code:
ls -lh | more
echo
echo
echo Please enter the directory that is the object of this exercise.
echo
read $directory
read Is $directory the patient in question? 
while :
do
	echo -n 'y: '
	read
	[[ <$REPLY meets criteria> ]] && break
	echo The patient in question was not found. >&2
done
When I ran the script, and inserted the directory name, the script appeared to hang, and no pdf files were generated.

I confirmed, by manually running each line in the script, that the script was in the correct location to search for the directory in question. What am I missing here? Thanks.
Couple of problems.

Firstly I don't know what
Code:
read Is $directory the patient in question?
is intended to do but it will try to read from the terminal (keyboard input) after the user has already responded to the prompt. If you had tried that at the command prompt (and given it a line of input from the keyboard) you would have got error "bash: read: `question?': not a valid identifier". Is "read" meant to be "echo"?

Secondly "<$REPLY meets criteria>" is not bash code, it is meta-code indicating that you must insert your own code to do the described task, such as the -e test on the directory suggested by lutusp.
 
Old 10-02-2009, 07:51 AM   #10
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
Hi there --

Thanks to everyone for the help. I was able to get the script to work, and I have enclosed it for anyone's perusal. The help was beneficial and greatly appreciated.

Code:
#!/bin/bash

# The purpose of this script is to go to the directory in question,
# and convert all postscript, .ps, files located there to .pdf files.

# Provide a list of the cases directory for the user.
cd /home/rsa/data/xknife_data/cases

echo CONVERSION OF POSTSCRIPT, .ps, FILES TO ADOBE ACROBAT, .pdf, FORMAT.
echo
echo
echo The patients in the cases folder will now be listed for your convenience.
echo
echo It is important that you make note of the exact spelling of the directory
echo in question in order for this process to work.
echo
echo
read -p "Press the enter key to continue..."

ls -1 | more
echo
echo
echo Please enter the directory that is the object of this exercise.
echo If you make a mistake here, hit CTRL-C to exit from the script,
echo and start over. 
echo
read directory
echo Is $directory the patient in question?

if [ -e $directory ]
then
echo "Patient directory found."
else
echo "Patient directory NOT found." 
fi

# Change to the directory in question
cd /home/rsa/data/xknife_data/cases/$directory

# Copy the .ps files to a local directory
cp -pr *.ps /tmp/ps2pdf

# Change to the temporary directory
cd  /tmp/ps2pdf

# List the files that were copied over the temporary directory
ls -l

# Convert the files in the temporary directory to .pdf format.
for i in *.ps; 
do ps2pdf "$i"; 
done

# Copy the .pdf files back to the original location
cp -pr *.pdf /home/rsa/data/xknife_data/cases/$directory

# Remove the files from the temporary directory
rm -rf *.ps
rm -rf *.pdf

echo The conversion of the postscript files to acrobat format is complete.
echo Use an ftp client application to retrieve the files from the patient directory.
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Defining loop statement for creating new file each time neo009 Linux - Newbie 6 04-12-2009 06:49 AM
Creating an application loop keysorsoze Linux - Software 6 01-19-2007 10:26 AM
How to loop a script? rollo Linux - General 3 12-21-2006 05:39 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:46 PM.

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