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 10-22-2009, 02:28 PM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Rep: Reputation: 39
Improving an existing script


Hi there --

I have a script running on one of our workstations that converts .ps files to .pdf versions using the ps2pdf binary. The text of the script 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 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
cd /tmp/ps2pdf
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.
I have been asked to add the following improvements to the script:

1. Enable the ability for the users to highlight the name of the directory of those listed, and then copy, and subsequently paste the name of it when prompted by the script.

2. Allow users to be able to backspace and correct the spelling of the directory when entering it at the prompt.

3. Allow users to reenter the name of the directory if they discover its spelling was typed in incorrectly at the prompt.

The first item is not absolutely necessary, although it would be nice to have available. The second and third items would be of great benefit to have.

I am not familar with the correct syntax to enable the items that I listed, so I was hoping someone could lend a hand here. Thanks.
 
Old 10-22-2009, 04:27 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
Quote:
Originally Posted by kaplan71 View Post
I am not familar with the correct syntax to enable the items that I listed
It would be good to see what you have tried yourself to come up with because if you're not somewhat familiar with scripting then you're also unlikely to understand any code in replies. Maybe look at some scripting guides first?

http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 10-23-2009, 03:19 PM   #3
kaplan71
Member
 
Registered: Nov 2003
Posts: 809

Original Poster
Rep: Reputation: 39
I did some further testing, and the code shown below is what I inserted into the script:

##########################################
read -p "Is the directory name that was entered spelled correctly <yes/no>"
if [ $REPLY = "no" ]; then
echo Reenter the patient directory name.
exit
fi
##########################################

This addition appears to work as far as the users are concerned.
 
Old 10-23-2009, 04:44 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
By way of using "exit" you exit the script. It would be more efficient to offer a way to loop over the name and change it, then move on. Also "the ability for the users to highlight the name of the directory of those listed" doesn't really exist in Bash but you could use a "select" case ('help select'), the "backspace and correct the spelling" should be in the loop mentioned before as could the "reenter the name of the directory" part.
You see the difference between a kludge and a work of Art is in the skilfull execution of composition and creativity. OK. I'm just kidding but it would be good to give thought to the execution flow of the script and chunk up parts. For instance for the OK / name change part you could have a function that only asks one question "Is this OK?" and only sets one variable. Once you got that part nailed you could move on. One such way is to use functions because they provide you with visually separate chunks of code and allow for reuse. Example:
Code:
function _selectDirName(){ # Search in PWD for subdirs, list and select:
 select MYDIRNAME in quit $(find "${PWD}" -type d); do
  case "${MYDIRNAME}" in 
   quit) break
         ;;
      *) USEDIRNAME="${MYDIRNAME}"; break
         ;;
  esac;
 done;
echo "In ${FUNCNAME}: select: variable USEDIRNAME = "${USEDIRNAME}"."
} # End _selectDirName

function _typeDirName (){ # User types dirname
 echo "Type in the name of the directory to use:"; read USEDIRNAME;
 readlink -f "${USEDIRNAME}" > /dev/null 2>&1 || { echo "Incorrect name \""${USEDIRNAME}"\", exiting."; exit 1; }
 USEDIRNAME=`readlink -f $USEDIRNAME`
 echo "In ${FUNCNAME}: read: variable USEDIRNAME = "${USEDIRNAME}"."
}
Declaring the function allows you to use it in another part of the script, like:
Code:
echo "Do you want to [s]elect or [t]ype a directory name?"; read DIRCHOICE
[ "${DIRCHOICE:0:1}" = "r" -O "${DIRCHOICE:0:1}" = "T" ] && typeDirName || selectDirName
[ -d "${USEDIRNAME}" ] || { echo "Incorrect name \""${USEDIRNAME}"\", exiting."; exit 1; }
* Note using if-else here isn't the best choice but only shown as example.
 
Old 10-23-2009, 07:40 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 355

Rep: Reputation: 165Reputation: 165
To
"2. Allow users to be able to backspace and correct the spelling of the directory when entering it at the prompt."
add '-e' to 'read' so that 'readline' is used to obtain the line.
It makes the left and right arrow keys work for editing instead of them just printing '^[[D' or '^[[C'

read -e 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
[SOLVED] [BASH]first script, suggestions for improving! RaptorX Programming 9 08-29-2009 05:11 PM
how do i call an already existing script in another script? Predatorian Programming 3 03-14-2008 08:49 AM
improving my script lpn1160 Linux - Newbie 4 06-19-2006 08:09 PM
Improving the startx script Woodsman Slackware 14 11-05-2005 08:08 AM
logrotate shell script help with existing script sridhar11 Debian 7 11-04-2005 04:11 AM

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

All times are GMT -5. The time now is 07:58 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