UNIX script question- Plse help guru's

Hi,
 
I'm in the midst of writing a UNIX script that sftp's files to an external host and am stuck with a problem. The problem is that the files created on my server as a order number that correlates to a sequence of directories on the remote host which is where the file should be ftp'ed.

Example:
My server filename:
myserver> /home/ 572011 O4MP ORDS 188406 V01 DT 20060608154548.txt

The 4th field (188406 ) in my filename correlates to a directory on the remote server where the file should be store. So on the remote server we should be sftp'ing this file to path

Remote Server:
remoteserver> /Test Documents/TestOrders/180001-190000/188001-189000/188401-188500/188406

The remote server has about 30+ directories and sub-directory ranges created to facilitate this file transfer. Remote server directory listings as range level and sub directories follow:
 100001-110000                  110001-120000
180001-190000                  270001-280000                  300001-310000                  320001-330000
330001-340000                  350001-360000                  370001-380000                  380001-390000
390001-400000                  400001-410000                  410001-420000                  420001-430000
430001-440000                  440001-450000                  450001-460000                  460001-470000
470001-480000                  480001-490000                  490001-500000                  500001-510000
510001-520000                  520001-530000                  530001-540000                  540001-550000
550001-560000                  560001-570000                  570001-580000                  580001-590000
590001-600000                  600001-610000                  

So another example would be that if we received a filename:
 /home/ 572011 O4MP ORDS 588406 V01 DT 20060608154548.txt (Note: 588406 is the directory to be stored on remote site as explained above)

How can I make the script smart enough for it to look at the forth fiels in my file name sftp it on the remote server path:
remoteserver> /Test Documents/TestOrders/580001-590000/588001-589000/588401-588500/588406

Your assistance would be greatly appreciated.

Thanks.
budrito12Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
JulieBouillonConnect With a Mentor Commented:
Try this one:
#!/bin/ksh

SOURCEDIR=/home/filedir

# let's go where our file resides
cd $SOURCEDIR

# we send each file found in our dir
for FILE in *
do
   # compute the destination directory
   typeset -i DIR3=$(echo "$FILE" | cut -f4 -d' ')
   typeset -i DIR2=$(echo $DIR3 | cut -c1-3)
   typeset -i DIR1=$(echo $DIR3 | cut -c1-2)
   TARGETDIR="/Test Documents/TestOrders/${DIR1}0001-$((DIR1+1))0000/${DIR2}001-$((DIR2+1))000/${DIR3}"

   # file transfer (I use scp instead of sftp, scp is as secure as is sftp, look at the man page for more details)
   scp "$SOURCEDIR/$FILE"  "testuser@10.242.XX.XX:$TARGETDIR"
   if [[ $? -ne 0 ]] ; then
      print "An error occured while transfering file $SOURCEDIR/$FILE"
   fi
done
0
 
budrito12Author Commented:
Please note this is for #!/bin/sh, #!/bin/csh or #!/bin/ksh and based on HP-UX 11i
0
 
JulieBouillonCommented:
This should you permit to do the trick:

---8<---
#!/bin/ksh

FILE="/home/ 572011 O4MP ORDS 588406 V01 DT 20060608154548.txt"

typeset -i DIR3=$(echo $FILE | cut -f5 -d' ')
typeset -i DIR2=$(echo $DIR3 | cut -c1-3)
typeset -i DIR1=$(echo $DIR3 | cut -c1-2)

echo "/Test Documents/TestOrders/${DIR1}0001-$((DIR1+1))0000/${DIR2}001-$((DIR2+1))000/${DIR3}"
--->8---

Hope it helps.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
budrito12Author Commented:
O.K very clever indeed and am very impressed...it works. Thanks heaps I'm also not just after solutions so can you please be kind enough to explain how and what your code does.

Once again thanks for helping out.
0
 
JulieBouillonCommented:

>FILE="/home/ 572011 O4MP ORDS 588406 V01 DT 20060608154548.txt"
Here you'll loop through your dir for each files. Since I just post a proof of concept a assigned a fixed values.

>typeset -i DIR3=$(echo $FILE | cut -f5 -d' ')
We create an "integer" which contains the fifth fields of $FILE. Fields are separated by blanks.
So now $DIR3 contains 572011.

>typeset -i DIR2=$(echo $DIR3 | cut -c1-3)
Another "integer" which contains the first 3 characters of $DIR3.
So now $DIR2 contains 572.

>typeset -i DIR1=$(echo $DIR3 | cut -c1-2)
The last "integer" we will used and which contains the first two characters of $DIR3.
So now $DIR1 contains 57.

>echo "/Test Documents/TestOrders/${DIR1}0001-$((DIR1+1))0000/${DIR2}001-$((DIR2+1))000/${DIR3}"
Here we construct the string.
The form ${DIR1} is the same as $DIR1 but the name of the variable must be enclosed by curly brackets. In fact if we wrote $DIR10001 instead of ${DIR1}0001, ksh would have tried to print out the content of a variable named DIR10001 and the results would have been an empty string.
The form $((DIR+1)) is the way we can do some basic arithmetics in ksh. In our cas, ksh is printing the result of 57+1 -> 58.


ksh is an amazing shell which allows you to make some elaborated stuff. I encourage you to familiarize yourself with ksh scripting:
 - http://quong.best.vwh.net/shellin20/
 - http://www.bolthole.com/solaris/ksh.html
0
 
budrito12Author Commented:
Many thanks for the above explanation. I have wriiten the below script incorportaing your directory calculation but seem to have an issue with the output. Basically for every file in directory "/home/filedir" find the remote directory name (as per your given algorithm) and sftp that file. Note that filename as given above has spaces and the output concatenates all the files into one commandline

SCRIPT:
#!/bin/ksh
tmp0=/tmp/a
INPUTFILE=/tmp/inputfile
cd /home/filedir
ls > $tmp0 2>/dev/null
FILE="/home/filedir/`cat $tmp0`"

#Number range algorithm
for filename in `cat $tmp0`
do
typeset -i DIR3=$(echo $FILE | cut -f4 -d' ')
typeset -i DIR2=$(echo $DIR3 | cut -c1-3)
typeset -i DIR1=$(echo $DIR3 | cut -c1-2)
done

TARGETOUT="/Test Documents/TestOrders/${DIR1}0001-$((DIR1+1))0000/${DIR2}001-$((DIR2+1))000/${DIR3}"

for filename in `cat $tmp0`
do
echo "cd $TARGETOUT" >> $INPUTFILE
echo "put "$FILE"" >> $INPUTFILE
echo "quit" >> $INPUTFILE
done

sftp -b $INPUTFILE testuser@10.242.XX.XX          > $tmp1 2>&1

Output of the above program is:
put /home/filedir/147258 12BL NOTE 259783 V01 DT 20060605161329.txt 457898 MISC NOTE 259793 V01 DT 20060609153346.txt 5
72011 O4MP ORDS 259789 V01 DT 20060608153450.txt
quit
cd /Test Documents/TestOrders/250001-260000/259001-260000/259783
put /home/filedir/147258 12BL NOTE 259783 V01 DT 20060605161329.txt 457898 MISC NOTE 259793 V01 DT 20060609153346.txt 5
72011 O4MP ORDS 259789 V01 DT 20060608153450.txt
quit
cd /Test Documents/TestOrders/250001-260000/259001-260000/259783
put /home/filedir/147258 12BL NOTE 259783 V01 DT 20060605161329.txt 457898 MISC NOTE 259793 V01 DT 20060609153346.txt 5
72011 O4MP ORDS 259789 V01 DT 20060608153450.txt
quit
..........................and repetitive....

Also note that the PUT wont work as the filename has space so treats each space as a filename. I've tried putting "" around the filename but can't seem to get it working.

Can you please advise where I'm going wrong.

Thanks
0
 
budrito12Author Commented:
Thanks for the above, I forgot to mention that in actual fact there was one other directory that I had to ftp to which I missed out but based on your explanation provided I managed to fix the issue. The script now looks like below but still has one outstanding issue. The TARGETOUT dir has a space (i.e Test Documents), I've tried putting in single & double quotes but it still does'nt cd into correct directory. I get the error message "scp: ambiguous target", any ide3s? Thanks for all your help to date.

#!/bin/ksh

SOURCEDIR=/home/filedir

# let's go where our file resides
cd $SOURCEDIR

# we send each file found in our dir
for FILE in *
do

# compute the destination directory
typeset -i DIR4=$(echo $FILE | cut -f4 -d' ')
typeset -i DIR3=$(echo $DIR4 | cut -c1-4)
typeset -i DIR2=$(echo $DIR4 | cut -c1-3)
typeset -i DIR1=$(echo $DIR4 | cut -c1-2)

TARGETDIR="/Test Documents/TestOrders/${DIR1}0001-$((DIR1+1))0000/${DIR2}001-$((DIR2+1))000/${DIR3}01-$
((DIR3+1))00/${DIR4}"


# file transfer
scp "$SOURCEDIR/$FILE"  "Test@10.242.xxx.xxx:$TARGETDIR"
echo "$SOURCEDIR/$FILE"  "ProjectMeerkat@10.242.xxx.xxx:$TARGETDIR"
if [[ $? -ne 0 ]] ; then
print "An error occured while transfering file $SOURCEDIR/$FILE"
fi
done
0
 
budrito12Author Commented:
please ignor the above, I've managed to fix the problem. Once again thanks for your help and explanation and I will award you the points.

The fix:
I put single quotes encapsulating $TARGETDIR...see below.

scp "$SOURCEDIR/$FILE"  "Test@10.242.xxx.xxx:'$TARGETDIR'"
echo "$SOURCEDIR/$FILE"  "ProjectMeerkat@10.242.xxx.xxx:'$TARGETDIR'"

rgds
0
 
budrito12Author Commented:
Hi,

Another question regarding the above, the 4th field in the filename has to be 6 digits and numeric. If it happens to be greater then  6+ digits how can I put a check in the script to move any filename with the 4th field greater then 6 digits to an error directory.

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.