Scripting issue. (388 Views)
Reply
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 1 of 11 (388 Views)
Accepted Solution

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 11 (388 Views)

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...
Respected Contributor
Greg Vaidman
Posts: 252
Registered: ‎09-12-2000
Message 3 of 11 (388 Views)

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.
Respected Contributor
Greg Vaidman
Posts: 252
Registered: ‎09-12-2000
Message 4 of 11 (388 Views)

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 11 (388 Views)

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...
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 6 of 11 (388 Views)

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. :(
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 7 of 11 (388 Views)

Re: Scripting issue.

I've been using echos instead of cp's in my testing stuff..
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 8 of 11 (388 Views)

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.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 9 of 11 (388 Views)

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.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 10 of 11 (388 Views)

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

Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 11 of 11 (388 Views)

Re: Scripting issue.

Thanks for the ideas.!!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.