#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    4
    Rep Power
    0

    Kornshell Script Help


    Hi,

    I have a Kornshell Script which I want to execute on a directory with a series of files;

    eg.

    XXXX.shp
    XXXX_MSLINK.shp
    YYYY.shp
    YYYY_MSLINK.shp
    ZZZZ.shp
    ZZZZ_MSLINK.shp
    ect.

    I want to add a condition to my for loop. I only want my for loop to run when I have files which can be translated against each other. For example,

    in my inputdirectory I only want my loop to run when I have files XXXX.shp and XXXX_MSLINK.shp ect. in the inputdirectory.

    I DO NOT want my for loop to run when I'm missing files. For example,

    in my input directory I DO NOT want my loop to run when I have XXXX.shp and no corresponding XXXX_mslink.shp. Or XXXX_mslink.shp and NO XXXX.shp. Instead I could have a message box pop up with a message to the user with the option to terminate the program.

    My Code:


    Code:

    Code:
    # enter do loop and translate each .shp file in the InputDirectory
    for file in $InputDirectory/*.shp
    do
        base=`basename $file.shp`
        if [ -e  ${base}_mslink.shp ] 
        then
     # strip off path
     fname=${file##*/}
     # strip off extention
     fname=${fname%.*}
     print |tee -a -i $WorkingDirectory/change.txt
     print "translating $fname.shp from shape to shape"  |tee -a -i $WorkingDirectory/change.txt
     print "======================"  |teje -a -i $WorkingDirectory/change.txt
     # *executes desired translation    
    fi
    done
    Any help would be greatly appreciated as this code doesnt seem to be working.

    Thanks,

    Beery
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    44
    Rep Power
    15
    Originally Posted by beery
    Hi,

    I have a Kornshell Script which I want to execute on a directory with a series of files;

    eg.

    XXXX.shp
    XXXX_MSLINK.shp
    YYYY.shp
    YYYY_MSLINK.shp
    ZZZZ.shp
    ZZZZ_MSLINK.shp
    ect.

    I want to add a condition to my for loop. I only want my for loop to run when I have files which can be translated against each other. For example,

    in my inputdirectory I only want my loop to run when I have files XXXX.shp and XXXX_MSLINK.shp ect. in the inputdirectory.

    I DO NOT want my for loop to run when I'm missing files. For example,

    in my input directory I DO NOT want my loop to run when I have XXXX.shp and no corresponding XXXX_mslink.shp. Or XXXX_mslink.shp and NO XXXX.shp. Instead I could have a message box pop up with a message to the user with the option to terminate the program.

    My Code:


    Code:

    Code:
    # enter do loop and translate each .shp file in the InputDirectory
    for file in $InputDirectory/*.shp
    do
        base=`basename $file.shp`
        if [ -e  ${base}_mslink.shp ] 
        then
     # strip off path
     fname=${file##*/}
     # strip off extention
     fname=${fname%.*}
     print |tee -a -i $WorkingDirectory/change.txt
     print "translating $fname.shp from shape to shape"  |tee -a -i $WorkingDirectory/change.txt
     print "======================"  |tee -a -i $WorkingDirectory/change.txt
     # *executes desired translation    
    fi
    done
    Any help would be greatly appreciated as this code doesnt seem to be working.

    Thanks,

    Beery
    Well, firstly your initial loop isn't just going through all XXXX.shp files, it's going through every file that ends in .shp (*.shp). Therefore, your loop will also attempt to process every XXXX_mslink.shp and therein lies at least the first problem.

    A simple solution to that would be to change your initial loop statement to the following:

    for file in $(ls -1 $InputDirectory/*.shp | grep -v _mslink)
    do
    <insert rest of script>

    In this way, you'll only try to process the 'plain' files and not the _mslink ones.

    Give that a shot and see what happens! Without more detail than "this code doesn't seem to be working" I can't really help more.

    Cheers!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    4
    Rep Power
    0
    Hi,

    Thanks for your reply. I may not have been specific enough in my first email. I want my loop to process every .shp file in my input directory which includes .shp and _MSLINK.shp files.

    What Im attempting to do is preform a change detection on the two files. Therefore XXXX.shp and XXXX_MSLINK.shp are both needed in order to preform my desired change detection.

    However, in my InputDirectory sometimes I may be missing one of the two files. For example, I may only have XXXX.shp and No XXXX_MSLINK.shp file to run my change detection comparision on.

    I would like the loop to run when I have both XXXX.shp and XXXX_MSLINK.shp in the my InputDirectory, and I dont want my loop to run when I cant preform a comparison because I'm missing one of either files.

    I hope this helps, id be happy to provide any more information if necessary.

    thanks,

    Bryan
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    44
    Rep Power
    15
    Originally Posted by beery
    Hi,

    Thanks for your reply. I may not have been specific enough in my first email. I want my loop to process every .shp file in my input directory which includes .shp and _MSLINK.shp files.

    What Im attempting to do is preform a change detection on the two files. Therefore XXXX.shp and XXXX_MSLINK.shp are both needed in order to preform my desired change detection.

    However, in my InputDirectory sometimes I may be missing one of the two files. For example, I may only have XXXX.shp and No XXXX_MSLINK.shp file to run my change detection comparision on.

    I would like the loop to run when I have both XXXX.shp and XXXX_MSLINK.shp in the my InputDirectory, and I dont want my loop to run when I cant preform a comparison because I'm missing one of either files.

    I hope this helps, id be happy to provide any more information if necessary.

    thanks,

    Bryan
    Yes, I understood that - that's what my last post was addressing! There are a couple of problems with your script, and I addressed the first one in my first reply. Here's another try at clarifying: Your script, as shown, checks for the existence of a matching _MSLINK.shp file in the section below:

    base=`basename $file.shp`
    if [ -e ${base}_mslink.shp ]
    then

    Therefore, with my change you're only processing the XXXX.shp files because your test involves checking for the existence of a matching ${base}_mslink.shp. Obviously, this is pointless if you're checking XXXX_mslink.shp as you know it exists, it's what you're processing! This would also give you a potential false positive and/or double processing, which I doubt you want.

    There is another syntax error in your code however that won't be helping. Your basename command is not stripping off the trailing .shp as your script seems to presume IE if you're processing a file XXXX.shp then your test will be checking if XXXX.shp_mslink.shp exists, which I suspect isn't what you're looking for. Try this instead, and note the all-important space between $file and .shp:

    base=`basename ${file} .shp`
    if [ -e ${Inputdirectory}/${base}_mslink.shp ]
    then

    So, combining my two suggestions will give you the following:

    for file in $(ls -1 ${InputDirectory}/*.shp | grep -v _mslink)
    do
    base=`basename ${file} .shp`
    if [ -e ${Inputdirectory}/${base}_mslink.shp ]
    then
    <etc>

    In pseudo-code-speak this is what it's doing:

    1) Create a list of all files in the Inputdirectory that end with .shp but NOT the _mslink files. We don't want them as we'll be checking to see if there's a matching _mslink file for the plain one. If either isn't there then you don't want to process them, as per your post. If there's a plain file but no _mslink then the test in the script further down will make the script skip it. If there's an _mslink but no plain one then it'll never make it into the first list and won't be processed (hence the grep -v)

    2) Set the variable ${base} to be the value of the filename without the trailing .shp. Note that the 'basename' command strips away the path to the file! Therefore, if you run this in any directory other than ${Inputdirectory} it'll never find the matching _mslink.shp file, as it'll be looking in the current directory. This is why I've added the ${Inputdirectory} to the ${base} for the test. It'd be easier to add in a command to cd ${Inputdirectory} at the beginning of the script or create a variable that combines the ${Inputdirectory} and ${base}, but I leave that up to you depending on how you want this to work.

    3) Now that we've got the basename of the plain .shp file (XXXX in our fictional example) we check for the existence of XXXX_mslink.shp. If found, we continue with the rest of your script.

    Mind, as you can see, with the basename command you really don't need any of the $fname manipulations you're doing later on in the script.

    There's more work to be done on this script, but I've got to leave *something* for you to do.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    4
    Rep Power
    0
    Thanks Bardon

IMN logo majestic logo threadwatch logo seochat tools logo