BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

Scripting issue.

Go to Solution
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.


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.
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/`
for dist in `echo $fn |awk '{print substr($1,1,3)}'`
export dist

case $dist in
export distpath="$wst"
export distpath="$mps"
export distpath="$esu"
export distpath="$plv"
export distpath="$ral"

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

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)'.


Respected Contributor

Re: Scripting issue.

replace everything from your first for loop with the following:

for fn in /mps-docs/mps/docs/app-staging/*
__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

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

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

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

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

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.


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. :(
Valued Contributor

Re: Scripting issue.

I've been using echos instead of cp's in my testing stuff..
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.
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.
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.

export distpath=/upload_resumes/rextest/
export source=/mps-docs/mps-docs/app-staging/rextest/

cd $source
for fn in `ls`
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

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

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

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

Valued Contributor

Re: Scripting issue.

Thanks for the ideas.!!