Operating System - HP-UX
1753936 Members
10129 Online
108811 Solutions
New Discussion юеВ

Scripting question - mkdir statement

 
SOLVED
Go to solution
rmueller58
Valued Contributor

Scripting question - mkdir statement

All, I have a script that i am having problems with inconsistent behavior,
if the "TRANSCRIPT Folder" exist the script will move a file per directive, I have an IF statement that says if the Folder for this APPID does not exist mkdir the Transcripts directory before copying the file.

I would like to ask for other eyes to see what problems exist in this script that maybe be disallowing this script to work. The top of the script is mostly defining variable the bottom is what seems to be causing my difficulty.. Any insight or thought appreciated.. This is a homegrown document management system for one of our districts HR departments.

#!/bin/bash
export destpath=/mps-docs/mps-docs/applicant/
export source=/mps-docs/mps-docs/transcript-staging/
export backuppath=/mps-docs/mps-docs/transcript-backup/
mv /tmp/mps_temp.log /tmp/mps_temp.log.`date +%Y%m%d`
touch /tmp/mps_temp.log
echo > /tmp/mps_temp.log
echo `mount |grep mps-docs ` >> /tmp/mps_temp.log
cd $source
for fn in `ls`
do
ls $source >> /tmp/mps_temp.log
echo Source Location $source >> tmp/mps_temp.log
echo Filename in Source Location $fn >> /tmp/mps_temp.log
echo Destination Path $destpath >> /tmp/mps_temp.log

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 tID=`echo $fn |awk -F_ '{print $4}'|cut -d. -f1`
export ext=`echo $fn |awk -F_ '{print $4}'|cut -d. -f2`
echo $fn |cut -d. -f2 | read ext

echo $fn >> /tmp/mps_temp.log
echo $destpath$appID >> /tmp/mps_temp.log
ls $destpath$appID >> /tmp/mps_temp.log
echo $destpath$appID/Transcripts >> /tmp/mps_temp.log
ls $destpath$appID/Transcripts/ >> /tmp/mps_temp.log


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

while [[ -f ${destpath}/${appID}/Transcripts/${distID}_${appID}_${docID}_${tID}.${ext} ]]
do
(( docID += 1 ))
if (( docID > 99 )); then
echo "ERROR: docID too high!"
exit 2
fi
done
#####################################################################
#### ADDED DOUBLE QUOTE AROUND TEST VALUE - 12-28-2008 RCM ESU#3 ####
#####################################################################
if [ -d "${destpath}/${appID}/Transcripts" ]
then
cp -f $fn ${destpath}/${appID}/Transcripts/${distID}_${appID}_${docID}_${tID}.${ext}
echo Transcript Folder Exists and Files copies shown >> /tmp/mps_temp.log
ls ${destpath}/${appID}/Transcripts >> /tmp/mps_temp.log
mv ${source}${fn} ${backuppath}/
echo Backup of original files moved from ${source}${fn} to ${backuppath}/ >>/tmp/mps_temp.log
else
mkdir ${destpath}/${appID}/Transcripts
cp -f $fn ${destpath}/${appID}/Transcripts/${distID}_${appID}_${docID}_${tID}.${ext}
echo Transcript Folder Create and Files copies shown >> /tmp/mps_temp.log
ls ${destpath}/${appID}/Transcripts >> /tmp/mps_temp.log
mv ${source}${fn} ${backuppath}/
echo Backup of original files moved from ${source}${fn} to ${backuppath} >>/tmp/mps_temp.log
fi
done

8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: Scripting question - mkdir statement

Hi:

One way to avoid surprises and quickly find problems is to have your scrip fail when a variable isn't set.

#!/usr/bin/sh
set -u
...

Another thing I like to do when doing 'mkdir's is to issue them as:

# mkdir -p

...such that all intermediate paths are created in one simple statement.

Regards!

...JRF...
rmueller58
Valued Contributor

Re: Scripting question - mkdir statement

James, Thanks as usual, I will try both. and see if I can catch the problem in output some how.



James R. Ferguson
Acclaimed Contributor

Re: Scripting question - mkdir statement

Hi:

I would also add that you can use perform an automatic 'chmod' to set the permissions of the directory when you make it, since I don't see an explicit 'umask' declaration in your script and thus you are at the mercy of what you inherit:

# mkdir -p -m 775 ${mydir}/{mysubdir1}/${mysubdir2}

Regards!

...JRF...
rmueller58
Valued Contributor

Re: Scripting question - mkdir statement

so make it look as such:

mkdir -p -m 775 ${destpath}/${appID}/Transcripts

James R. Ferguson
Acclaimed Contributor

Re: Scripting question - mkdir statement

Hi (again):

> so make it look as such:

mkdir -p -m 775 ${destpath}/${appID}/Transcripts

Yes, that's the idea, although you may want an absolute path:

# mkdir -p -m 775 /${destpath}/${appID}/Transcripts

The permissions are entirely your choice, too.

Regards!

...JRF...

rmueller58
Valued Contributor

Re: Scripting question - mkdir statement

James, This script is scheduled to run as a cron I keep forgetting about full path to executables when running as cron.

I will fix these as well Thanks for the assist.. I will monitor the outcome and close this thread accordingly..

dirk dierickx
Honored Contributor

Re: Scripting question - mkdir statement

setting the shell in '-x' will allow you to see what it is doing, great tool for troubleshooting (it's a kind of debug mode for shell scripting).
rmueller58
Valued Contributor

Re: Scripting question - mkdir statement

Thanks for the help.. I think maybe finally got this fixed.