1748081 Members
5140 Online
108758 Solutions
New Discussion юеВ

Scripting issue.

 
SOLVED
Go to solution
rmueller58
Valued Contributor

Scripting issue.

I have a situation which requires checking for an existing file name in a directory then I need to copy a file with a new document ID

the format of the file name is fairly simplistic.

__.ext

the customer puts file in a staging area, I have a destination folder

What I've done is defined some variables and am trying to parse into the file name to see the DOCID in the destination folder exists compared to the staged files, and if it exists then I want to increment the DOCID by one during the cp.

my script thus far is fairly simplistic, I have to get it running in cron so I don't have to baby sit a user process..

I have a CASE statement that looks at the file name and pulls the DISTID so I can apply the correct destination folder.
#!/bin/bash
export p1=/upload_resumes/rextest/
export p2=/upload_resumes/backup/
export p3=/mps-docs/mps-docs/app-staging/rextest/


for fn in `ls /mps-docs/mps-docs/app-staging/`
do
for dist in `echo $fn |awk '{print substr($1,1,3)}'`
# DISTRICT SPECIFIC INFORMATION
do
export dist

case $dist in
wst)
export distpath="$wst"
;;
mps)
export distpath="$mps"
;;
esu)
export distpath="$esu"
;;
plv)
export distpath="$plv"
;;
ral)
export distpath="$ral"
;;
*)
;;
esac

Then i need to look at the file name again to pull our the DOCID and check the DOCID against the destination folder. The customer has thus far given the APPID of NEW or ADD, which makes the cp a bit more problematic.

the file name generally will look like this.

mpsplus_1234_1.pdf or mpsplus_1234_2.pdf the _2.pdf can be as many as _99.pdf

I am drawing a blank on my scripting brain for some reason today.

Any scripting guys with some ideas I'd appreciate it.

Rex Mueller
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

Re: Scripting issue.

Hi Rex:

Give a file name in the variable FILE:

[ -f "${FILE}" ] && echo "exists" || echo "does not exist"

...will print "exists" or "does not exist"

See the manpages for 'test(1)'.

Regards!

...JRF...
Greg Vaidman
Respected Contributor

Re: Scripting issue.

replace everything from your first for loop with the following:

for fn in /mps-docs/mps/docs/app-staging/*
do
__echo $fn | cut -d. -f1 |
____IFS=_ read distID appID docID
__echo $fn | cut -d. -f2 | read ext

__eval distpath="$dist"
__if [[ -z $distpath ]]; then
____print "ERROR: invalid distID $distID"
____exit 1
__fi

__while [[ -f ${distpath}/${distID}_${appID}_${docID}.${ext} ]]
__do
____(( docID += 1 ))
____if (( docID > 99 )); then
______print "ERROR: docID too high!"
______exit 2
____fi
__done

__mv $fn ${distpath}/${distID}_${appID}_${docID}.${ext}
done


---
you may want to add some echo statements to see that your variables are set correctly, and you may want to do "typeset -Z2 docID" to make sure you always have a 2 digit docID (it adds the leading zero for #<10).

the eval statement saves you from your big case statement, and the following if validates that the env var for the distID was set.
Greg Vaidman
Respected Contributor

Re: Scripting issue.

sorry, the eval line should read
eval distpath="\${$dist}"

the leading "_" on each line are to simulate spaces, so you the forum doesn't mess up the indentation of the script
James R. Ferguson
Acclaimed Contributor

Re: Scripting issue.

Hi Greg:

You can (recently) chekc the "Retain Format" box at the bottom of your post to cause leading spaces to be retained for formatting scripts.

Regards!

...JRF...
rmueller58
Valued Contributor

Re: Scripting issue.

Thanks guys, let me take a look..

greg's idea looks like it will probably do the trick but I was having a major brain drain this morning.

Cold Wendy's fries don't help matters either. :(
rmueller58
Valued Contributor

Re: Scripting issue.

I've been using echos instead of cp's in my testing stuff..
rmueller58
Valued Contributor

Re: Scripting issue.

what shells does IFS work with? I am using a bash shell..

here is what I am trying,

I am defining some of the more static variables first such as

export SOURCEDIR="/mps-docs/mps-docs/app-staging/"
export DESTDIR="/web-folder/"

This script is driven by one of my school districts, hence the district ID..

I define the distid in a case statement. the dbname is plus so for example mpsplus is for mps district. that is simple enough.. As I only have the potential of 11 potential districts the case is a small piece of the pie.

the basic concern is
moving files from a static directory to another static directory, but looking at the APPID and DOCID.

IF THE APPID ID exists in the DESTINATION FOLDER, with a specific DOCID then increment the DOCID + 1 and retain the extension.

It looks as though if I define the FILENAME first, then parse for the sub variables within the document name and increment the DOCID with portions from you guys I can probably get it to work.

I will muck with it today, and see if I can get it running. Thanks again.
rmueller58
Valued Contributor

Re: Scripting issue.

I ran through it once with greg's script, and it put a file in my destination folders with _. it didn't pass the IFS information (or maybe the IFS portion isn't reading the filename correctly.
rmueller58
Valued Contributor

Re: Scripting issue.

Guys this is what I came up with, and it seems to be working.. Thanks much for the muse time.

#!/bin/bash
export distpath=/upload_resumes/rextest/
export source=/mps-docs/mps-docs/app-staging/rextest/

cd $source
for fn in `ls`
do
echo $fn
echo $distpath
export distID=`echo $fn |awk -F_ '{print $1}'`
export appID=`echo $fn |awk -F_ '{print $2}'`
export docID=`echo $fn |awk -F_ '{print $3}'|cut -d. -f1`
export ext=`echo $fn |awk -F_ '{print $3}'|cut -d. -f2`
echo $fn |cut -d. -f2 | read ext

echo $distID
echo $appID
echo $docID
echo $ext
read


if [[ -z $distpath ]]; then
echo "ERROR: invalid distID $distID"
exit 1
fi

while [[ -f ${distpath}/${distID}_${appID}_${docID}.${ext} ]]
do
(( docID += 1 ))
if (( docID > 99 )); then
echo "ERROR: docID too high!"
exit 2
fi
done

cp $fn ${distpath}mpsplus_${appID}_${docID}.${ext}
done