Results 1 to 8 of 8

Thread: Sript to copy

  1. #1
    Join Date
    Nov 2012
    Beans
    4

    Question Sript to copy

    Hi,
    I am new to Ubuntu and its terminal. Currently I am doing a simulation project and I have to work on two different folders lets say "Folder1" and "Folder2". Simulation in "Folder1" gives "code.bin" file and I have to copy this file to "Folder2" and run another simulation in that folder. It is waste of time to copy "code.bin" file every time to another folder. So i need to code a script that does that automatically for me such that I can immediately switch to "Folder2" from "Folder1" and start the simulation. So any suggestions friends?

    Regards

  2. #2
    Join Date
    Jan 2007
    Beans
    6,537
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: Sript to copy

    You could just write a script that runs the simulation, then copies the output to the second location before starting the second simulation there.

    Basic BASH scripting.

  3. #3
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Sript to copy

    Does Folder2's name ever change, or is it always the same?

    As a start, you could do something like this:

    Code:
    #!/bin/bash
    
    sim2Dir=$1
    
    if [ -d "../$sim2Dir" ]; then
            if [ -e "code.bin" ]; then
                    cp "code.bin" "../$sim2Dir" && eval "../$sim2Dir/code.bin"
            else
                    printf "The file 'code.bin' does not yet exist!"
                    exit 1
            fi
    else
            printf "There is no directory called: %s\n" $sim2Dir
    fi
    The script will check for "folder2" and if it exists, it will cp 'code.bin' to that folder and execute it (I used eval because I don't know what kind of script it is...you might want to change how it's executed there). If folder2 does not exists, then you get an error and the program exits. You could change that error section to make it create folder2 and so on.

    To use the script, you can make it executable:

    Code:
    chmod +x cp_run.sh
    And then run it like this:

    Code:
    ./cp_run.sh folder2
    Folder2 should be the actual name of the folder where you want to copy and run the 'code.bin' script.

  4. #4
    Join Date
    Nov 2012
    Beans
    4

    Re: Sript to copy

    Thank you, drmrgd. But I am sorry that i did not understand few things in the suggested script. Does sim2Dir means "Folder2" (Yes it is same everytime)? And does
    Code:
    " .../$sim2Dir"
    means that i have to give full path of "folder2".

  5. #5
    Join Date
    Nov 2012
    Beans
    4

    Re: Sript to copy

    I tried giving my "folder2" name in place of "sim2Dir" and tried running the script. It displayed following message:
    Code:
     ./cpy_script.sh: line 7: ..//code.bin: Permission denied

  6. #6
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Sript to copy

    Quote Originally Posted by PKJack View Post
    Thank you, drmrgd. But I am sorry that i did not understand few things in the suggested script. Does sim2Dir means "Folder2" (Yes it is same everytime)? And does
    Code:
    " .../$sim2Dir"
    means that i have to give full path of "folder2".
    Since I wasn't sure if 'folder2' was always going to have the same name, I elected to pass it to the script as an argument. Within the script, then I was passing that argument into the variable called $sim2Dir. We can simplify things if the folder name is always exactly the same by just hard coding that folder name in the script. The way I did it, though gives you a little more flexibility if the name does happen to change for one reason or another. Here's the change for a hardcoded "Folder2" directory:

    Code:
    #!/bin/bash
    
    if [ -d "../Folder2" ]; then
            if [ -e "code.bin" ]; then
                    cp "code.bin" "../Folder2" && cat "../Folder2/code.bin"
            else
                    printf "The file 'code.bin' does not yet exist!"
                    exit 1
            fi
    else
            printf "There is no directory called: 'Folder2'!\n" 
    fi
    In regard to the path question, I went based on the assumption that you would be running the script from Folder1 every time based on the conditions that you mentioned above. Without knowing more about what you're doing and how you have your data stored, it's hard to know what the real path would be; I would prefer more rigid, full paths to be on the safe side, though. You can tailor the paths to meet your needs. Just change the lines that say "../Folder2" to match the path and the name of the folder as you wish. Then (after making it executable of course), run it by typing:

    Code:
    ./name_of_script.sh

  7. #7
    Join Date
    Nov 2012
    Beans
    4

    Lightbulb Re: Sript to copy

    Solved! Thank you very much for the helpful suggestions It made my work lot easier!

    Regards

  8. #8
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Sript to copy

    Quote Originally Posted by PKJack View Post
    Solved! Thank you very much for the helpful suggestions It made my work lot easier!

    Regards
    Great! Glad to help!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •