Need to decrypt a file in a directory (SHL script)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to decrypt a file in a directory (SHL script)
# 1  
Old 11-22-2010
Need to decrypt a file in a directory (SHL script)

I need to decrypt a file in a directory, I need to write a shl scrip & cron job
How I find the files in the directory:
the file is like this:
Code:
dailypayments_sfs_payment_201011151800.dat -d

The decrypt command:
Code:
gpg -o dailypayments_sfs_payment_201011151800.dat -d

20101115 (the date thar the file arrive) and 1800 (it seems that the file always arrive at the same time 1800)
the date will be the sysdate (20101115)
Afrer I capture the file I need it to encrypted then save it in a directory (archieves), again I can have more than one file in a giving day
Code:
gpg -o dailypayments_sfs_payment_201011151800.dat -d

Thank you

Last edited by Scott; 12-01-2010 at 08:15 PM.. Reason: Formatting; Code tags
# 2  
Old 11-22-2010
You might architect this a bit first.
  1. Make a directory for each of: incoming, decoded already and the decoded outputs. Maybe an error directory for failures.
  2. email about failures is nice, and a daily email report of decodes.
  3. Run the scripts on cron.
  4. Use fuser to ensure the file is not already being decoded by a process of the last cron interval, or still being downloaded.
Code:
(
cd incoming
for f in *
do
 if [ "$(fuser #f 2>/dev/null)" != "" ]
 then
  echo $f is in use.
  continue
 fi
 (decode <$f >../output/$f.decoded
   if [ $? = 0 ]
   then
    mv $f ../processed/$f.done
   else
    echo error decoding $f
   fi
  ) &
done
)

# 3  
Old 11-22-2010
are you sure the file name end with -d ? If the file end with dat, you can use find command

Code:
find . -type f -name "*.dat" -exec gpg -o {} -d \;

# 4  
Old 11-22-2010
Quote:
Originally Posted by rdcwayx
are you sure the file name end with -d ? If the file end with dat, you can use find command

Code:
find . -type f -name "*.dat" -exec gpg -o {} -d \;

I believe -d is being used as the decrypt option to gpg.
# 5  
Old 11-23-2010
Not, the file is like this
dailypayments_sfs_payment_201011151800.dat

where 1800 is always the time (6pm) and 20101115 (date) yyyymmdd
and this is dailypayments_sfs_payment (always the same name)
It can be more than one file in there...

---------- Post updated at 11:13 AM ---------- Previous update was at 08:27 AM ----------

What about this: (sorry new at this stuff),
I don't have to move the process file to another directory there is another process that will do that, I just to encrypt the file and leave in the $LocalDir, so the other process will take over

will this work? do the encryption?
Code:
# Start Decrypt Process
# Determine if there is a file to process 
 $FileName find . -type f -name "*.dat.pgp" >/dev/null 2>&1
if [$? !=0]
then 
exit 
fi
for $FileName  in *
do 
  if [ "$(fuser $FileName 2>/dev/null)" !="" ]
  then 
     echo $FileName is in use 
     continue 
fi
   ( -gpg -o #FileName -d  > $LocalDir
     if [ $? = 0] 
    then 
      echo file decrypted 
  else 
         echo error decoding $FileName
     fi
     ) done 
   
# End of Decrypt process


Last edited by Scott; 12-01-2010 at 08:13 PM.. Reason: Code tags, please...
# 6  
Old 11-23-2010
Use the Code tags button left of <> above so it looks like this:
Code:
$!/usr/bin/ksh
 .
 .
 .
# Start Decrypt Process
# Determine if there is a file to process 
for $FileName in *.dat.pgp
do
  if [ "$FileName" = '*.dat.pgp' ] # no files
  then 
    exit
  fi
 
  if [ "$(fuser $FileName 2>/dev/null)" !="" ]
  then 
    date "+%Y-%m-%d %H:%M:%S $FileName is in use"
    continue 
  fi

  gpg -o #FileName -d > $LocalDir

  if [ $? = 0] 
  then 
  else 
    date "+%Y-%m-%d %H:%M:%S error decoding $FileName"
  fi
done

Find is overkill if the files are all in one or a few directories. I never liked the find -exec option, as you do not have good error handling when unattended. You can "find | while read f ; do done" and check for errors, have a separate log for each conversion, etc. Not sure what "$FileName find" does! You can detect unexpanded wild cards easier as above. Not sure what the gpg command line is up to, but most of the line is a weird #comment, work on that. You, too deserve structural indentation and blank lines for easy visual separation of complex constructs.
# 7  
Old 12-01-2010
I am getting this errors:
Code:
spinel:/home/nelnet$ sh payfile_decrypt2.shl 'PRCT'
payfile_decrypt2.shl[31]: LocalDir:  not found.
payfile_decrypt2.shl[41]: FileName:  not found.
payfile_decrypt2.shl[84]: $FileName: This is not an identifier.

when I do this from the command line
Code:
cd /home/nelnet/NBS_banner_kit/banner/eodrec_component

it brings me to the right directory
This is what I have
Code:
LocalDir =  "cd /home/nelnet/NBS_banner_kit/banner/eodrec_component";
FileName = ls *1800*.dat.pgp;

#-------------------------------------------------------------------------------#
# Start Decrypt Process
# Determine if there is a file to process
for $FileName in *.dat.pgp
do
  if [ "$FileName" = '*1800*.dat.pgp' ] # no files
  then
    exit
  fi
  if [ "$(fuser $FileName 2>/dev/null)" !="" ]
  then
    date "+%Y-%m-%d %H:%M:%S $FileName is in use"
    continue
  fi
  gpg -o #FileName -d > $LocalDir
  if [ $? = 0]
  then
     "Sucess decrypting the file"
  else
    date "+%Y-%m-%d %H:%M:%S error decoding $FileName"
  fi
done
  if [ $? = 0]
  then
      echo "Sucessfully decoding File Name:"  $FileName   >>${LF1} 2>>${lf2}
          mailx -s "sucesfully decode the file: " $FileName  $EMAIL_ADDRS  < ${LF1}
  else
    echo "Error decoding  file Name:"  $FileName   >>${LF1} 2>>${lF2}
        mailx -s "Error decoding File:"    $FileName   $EMAIL_ADDRS  < ${LF1}
  fi
done

when I execute this from the command line
Code:
     ls *1800*.dat.pgp;

I get the right file, the one that I want to decrypt

Last edited by Scott; 12-01-2010 at 08:22 PM.. Reason: Formatting; Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Encrypt Password file and decrypt in a shell script

Hi All, I have stored Oracle database passwords in a hidden file - .pass_file. My shell script reads the hidden file, gets the password and then logs in to the Oracle database and runs some SQL script. My requirement is: I need to provide the shell script to be executed by someone else. So,... (1 Reply)
Discussion started by: sunpraveen
1 Replies

2. Shell Programming and Scripting

Problems coping a file in a shl script

I have the following in a shl script: SCRIPT_PATH="/u01/app/banner/test/skid/plus/"; FILE_PATH="/nfs/mercury/u03/banner/test/skid/log"; LIST_FILE_PATH="/u01/banjobs/TEST"; SCRIPT_NAME="szpcal1.sql"; FILE_NAME='new_applicant_list'; I want to copy the file FILE_NAME to LIST_FILE_PATH ... (10 Replies)
Discussion started by: rechever
10 Replies

3. Shell Programming and Scripting

gzip and encrypted in a shl script

After I move the file to a directory, I need to gzip and encrypted. I never do this in a shl script, I do it from the command line and it works.. cd /home/nelnet spinel:/home/nelnet$ gpg -e 2010_11_07_05_11_xxxxxx_bills.dat.gz `/home/nelnet/.gnupg/gpg.conf' `/home/nelnet/.gnupg/gpg.conf'... (3 Replies)
Discussion started by: rechever
3 Replies

4. Shell Programming and Scripting

last file in a directory in a shl script

I want to capture a last file in a directory in a shl scrip Cd to the directory $last ls -1 *the_bills.dat* | tail -1 This is not problem, but I need to copy that file to another directory, but in order to be able to copy to that directory, I need to su in unix, I am sure my sistem... (2 Replies)
Discussion started by: rechever
2 Replies

5. Shell Programming and Scripting

This code works in the command line but not in a shl script

When I run this code from the command line works spinel.middlebury.edu:/u02/sct/banner/bandev2/middlebury/shl:DEV2$ ls ef* eftseq.dat spinel.middlebury.edu:/u02/sct/banner/bandev2/middlebury/shl:DEV2$ file_seq=$( < eftseq.dat) ... (1 Reply)
Discussion started by: rechever
1 Replies

6. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

7. Shell Programming and Scripting

Reading a file (one record) in a SHL script

I am trying to read a file in a shl script (only one record) and stored in a variable file_number I got the following read -u $BANNER_HOME/xxxxxxx/misc/EFTSQL.dat file_number file_number2 = $file_number + 1 echo $file_number2 > $BANNER_HOME/xxxxxx/misc/EFTSQL.dat EOF It is not working... (2 Replies)
Discussion started by: rechever
2 Replies

8. UNIX for Advanced & Expert Users

Decrypt a GPG file through shell script

Hi, I'm trying to decrypt a gpg file thorugh a shell script. But i' could'nt. My script is , -sh-3.1$ cat test_gpg.sh #!/bin/ksh echo " Hello, iam testing GPG" gpg prabhu.txt.gpg <<EOF prompt prabhu EOF exit 0 The file i'm trying to decrypt is prabhu.txt.gpg and my passphrase is... (1 Reply)
Discussion started by: apsprabhu
1 Replies

9. Shell Programming and Scripting

shl script capture a file

need to be able to capture a file with the following conditions: The filenames are, for example, 3526_332840.dat, where 3526 is constant, and 332840 is a sequential number which is always a couple hundred greater than the previous day's file. I want to be able to change this script to acoomplish... (1 Reply)
Discussion started by: rechever
1 Replies

10. Shell Programming and Scripting

Encrypt and Decrypt script

Dear Experts, I am using one script name :volume.sh and its written in bash shell script. I just want to encrypt the script so that any one else cannot see it. please tell me the commands how to encrypt the script as well as to decrypt it. Regards, SHARY (9 Replies)
Discussion started by: shary
9 Replies
Login or Register to Ask a Question