I have written a plsql procedure using utl file to create files in the temp directory in Unix.

I am sending the names of the files to a shell script to mail these files and delete after mailing from temp directory, it is working good but when two people running the same request with the same files, the files are replaced and deleted by the first program and the second program not having any files in the directory to mail.

I decided to create file names_request_id to make file names unique, but my actual requirement is to get the files with the same name with out request_id to person who is receiving email.

I created Incompatibility for the program with the same program but they want me to take away the incompatibility to save time.

What other options do I have?

For your reference i copied my whole shell script and added ####### symbol to denote the location of the code to be modified.

The files are moved to the created directory and the file names have reuest_id-filename.extension so that request_id and the '-' must be removed and the actual filename should be there. name of the file in the database: 23456663-abcd.pdf required name of the filename : abcd.pdf

param=`echo $*`
#getting input values
echo "params : $param"

v_temp=`echo $param  | cut -d '"' -f 1 `
v_temp=`echo $v_temp | cut -d '=' -f 2 `
v_temp=`echo $v_temp | cut -d ' ' -f 1 `
#now v_temp has the request_id

echo "temp file is : $v_temp"

#split the parameter string with " as seperator
v_email_list=`echo $param | cut -d '"' -f 8 `
v_file_list=`echo $param | cut -d '"' -f 10 `
v_subject=`echo $param | cut -d '"' -f 12 `
v_mail_content=`echo $param | cut -d '"' -f 14 `
v_reply_to=`echo $param | cut -d '"' -f 16 `
v_delete_attachment_files=`echo "$1"|cut -d'"' -f18`
v_outputfile_path=`echo "$1"|cut -d'"' -f20`
v_outputfile=`echo "$1"|cut -d'"' -f22`
v_request_id=`echo "$1"|cut -d'"' -f24`
#v_rename_file=`echo $param | cut -d '"' -f 24 `
echo "email list :" $v_email_list
echo "file list :" $v_file_list
echo "subject :" $v_subject
echo "mail_content :" $v_mail_content
echo "reply_to :" $v_reply_to
echo "delete_attachment_files :" $v_delete_attachment_files
echo "outputfile_path :" $v_outputfile_path
echo "outputfile :" $v_outputfile
echo "request_id :" $v_request_id

v_email_list=`echo $v_email_list |tr ";" " "`
v_file_list=`echo $v_file_list |tr ";" " "`

cd /

mkdir /tmp/$v_request_id
cp $v_outputfile_path /tmp/$v_request_id/$v_outputfile

mv /tmp/$v_request_id* /tmp/$v_request_id/ 
cd /tmp/$v_request_id/

###### this is the place i need to rename the files that are moved to a request_id dir
################# mv -- "34178248-2640006.pdf" "${34178248-2640006.pdf}" 

v_mail_content =`basename $v_mail_content`
cat $v_mail_content > /tmp/${v_temp}.tmp

echo "File List : $v_file_list"

for i in ${v_file_list}; do
    echo "File to attach before parsing $i"
    v_attachment_name=`echo $i |awk '{ if ( index($1,":")> 0) { print substr($1,index($1,":")+1) } }'`
    v_attachment_file=`echo $i |awk '{ if ( index($1,":")> 0) { print substr($1,1,index($1,":")-1) } }'`

    echo "Checking attachment name after parse :"$v_attachment_name
    if [ "$v_attachment_name" = '' ]
      v_attachment_name=`basename $i`
      echo "base name is " $v_attachment_name

    /usr/bin/uuencode $v_attachment_file $v_attachment_name >> /tmp/${v_temp}.tmp
echo "Loop done"

if [ "${v_reply_to}" = '' ]
  mailx -s "${v_subject}" "${v_email_list}" < /tmp/${v_temp}.tmp
#echo "something" | mailx -s "${v_subject}" ${v_email_list} < /tmp/${v_temp}.tmp
  mailx -r "${v_reply_to}" -s "${v_subject}" "${v_email_list}" < /tmp/${v_temp}.tmp
#echo "something" | mailx -r "${v_reply_to}" -s "${v_subject}" ${v_email_list} < /tmp/${v_temp}.tmp

echo "mailing done"

#delete the temporary files
  echo "deleting file /tmp/${v_temp}.tmp"
rm /tmp/${v_temp}.tmp

if [ "${v_delete_attachment_files}" = 'Y' ]
    echo "deleting attached files -start "
    #Loop through to identify all the attachments and delete
    for i in ${v_file_list}; do
        echo $i
        v_attachment_name=`echo $i |awk '{ if ( index($1,":")> 0) { print substr($1,index($1,":")+1) } }'`
        v_attachment_file=`echo $i |awk '{ if ( index($1,":")> 0) { print substr($1,1,index($1,":")-1) } }'`

        echo "Check :"$v_attachment_name
        if [ "$v_attachment_name" = '' ]
          v_attachment_name=`basename $i`
          echo "base name is " $v_attachment_name

        echo "deleting file $v_attachment_file"
        rm $v_attachment_file
    #End Loop through to identify all the attachments and delete

echo "rm done"

An alternate solution would be to create a temporary directory based on the process ID of the request, dump the files to that directory (perhaps via a cd command before generating the files?), then mail them from there. That way, the second job, with a different PID, would be operating in a different directory. I'm not sure how that would play out with the SQL bits, but that's where I would start my attack from.

  • Thanks for the quick reply... I tried that way, i created the files with request_id-filename and created a directory named request_id and now the problem is how to rename the files removing request id from the file name... – sam Jul 3 '13 at 14:03
  • i moved the files into the request_id directory and now have to rename the files .... i am updating my script in the question and thanks again for your time – sam Jul 3 '13 at 14:04

If you're just looking to create a uniquely named temporary file and/or directory you can use the mktemp command in your script.

To create a randomly named file and/or directory:

# file
$ mktemp

$  ls -l /tmp/tmp.4t0pSU5g3i
-rw------- 1 root root 0 Jul  3 11:05 /tmp/tmp.4t0pSU5g3i

# directory
$ mktemp -d

$ ls -ld /tmp/tmp.WixJWjOPBb
drwx------ 2 root root 4096 Jul  3 11:04 /tmp/tmp.WixJWjOPBb

If you'd like it to only suggest a name without actually doing the creation you can pass it the --dry-run or -u switch.


  • Thanks for your reply i have a unique request id to create the directory and i moved the files into that directory, but the file names need to be modified, so that the unique number must be trimmed from my file name – sam Jul 3 '13 at 15:16

Changing my answer as per update of the question.

If you just want to change the filenames then a simple way would be this:

filelist=`ls *.pdf`
for i in $filelist
    mv $i `echo $i|cut -d "-" -f 2`

From what I understood you have a list of files, as such, just iterate this part to remove the requestid from the beginning of the name.

  • thanks for you reply... i am not good at scripting.... i dont have any idea about lock. i Edited my question to give you more details and i also added the shell script which i am using.... can i use in my script is it possible – sam Jul 3 '13 at 14:18
  • I have a doubt, this procedure sounds great to me, but if two scripts are running, then there might be dead lock i guess, i am not sure – sam Jul 3 '13 at 14:42
  • Deadlock could occur if someone cancels the script between creating the lock file and removing it, otherwise you should be fine, the work around to robust this more would trap the signal into the script and treat them accordingly. – BitsOfNix Jul 4 '13 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.