Find the answer to your Linux question:
Results 1 to 6 of 6
Sorry for the noob question but im learning at the moment im using this and it works fine #!/bin/sh #add files if missing if [ -f /usr/local/destination/file1 ] then echo ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3

    Simple Shell Script


    Sorry for the noob question but im learning

    at the moment im using this and it works fine

    #!/bin/sh

    #add files if missing

    if [ -f /usr/local/destination/file1 ]
    then
    echo "Nothing to fix! file1 exists"
    else
    cp /usr/local/source/file1 /usr/local/destination/
    cp /usr/local/source/file2 /usr/local/destination/
    cp /usr/local/source/file3 /usr/local/destination/
    cp /usr/local/source/file4 /usr/local/destination/
    fi


    but if the folder "destination" is missing i get a failed to copy message ofc
    which is fine as if the folder isnt there i dont want the files copied
    but would prefer to do it without failed copy messages so its cleaner

    I want to
    check for file1 if it exists abort
    check for destination folder if doesnt exist abort
    else copy files

    Im not that good with scripts

    can someone point me in the right direction plz

  2. #2
    Just Joined!
    Join Date
    Jul 2012
    Posts
    5
    Sure. What you're looking for is: [ -d /usr/local/destination ] - It checks for the existence of a directory.

    Also, you can eliminate the "file existence" branch by saying [ ! -f /usr/local/destination/file1 ] - The "!" is the NOT condition, so it checks for the absence of the file (in this case).



    if [ ! -f /usr/local/destination/file1 ]; then
    if [ ! -d /usr/local/destination ] ; then
    echo "Destination folder does not exist."
    exit 1
    fi

    <do your copy here>
    exit 0
    fi

    If you wanted to get a little bit fancy, you could do something like this:

    DEST=/usr/local/destination
    SRC=/usr/local/source

    if [ ! -d $DEST ]; then
    echo "Destination directory: $DEST does not exist."
    exit 1
    fi

    if [ ! -f $DEST/file1]; then
    cp $SRC/file1 $DEST
    exit 0
    fi

    Another option is to use the command "cp -n" - which will not overwrite files if the file exists.
    Last edited by linuxcoder; 07-25-2012 at 11:02 AM.

  3. #3
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3
    thanks its sort of working

    but the script has alot of other scripts in it

    and is exiting out at the exit.. when i want it to process everything
    and its not pratical to move it to be the last script

    #many more fixes above this line

    #add files if missing

    DEST="/usr/local/destination"
    SOURCE="/usr/local/source"

    if [ ! -d "$DEST" ]; then
    echo "destination Not Installed"
    exit 1
    fi

    if [ ! -f "$DEST"/file1 ]; then
    cp "$SOURCE"/file1 "$DEST"
    cp "$SOURCE"/folder1/* "$DEST"/folder1/
    cp "$SOURCE"/folder2/file3 "$DEST"/folder2/
    cp "$SOURCE"/folder2/file4 "$DEST"/folder2/
    echo "files restored"
    exit 0
    fi


    #many more fixes below this line
    Last edited by SarahH; 07-25-2012 at 02:03 PM.

  4. #4
    Just Joined!
    Join Date
    Jul 2012
    Posts
    5
    Quote Originally Posted by SarahH View Post
    thanks its sort of working

    but the script has alot of other scripts in it

    and is exiting out at the exit.. when i want it to process everything
    and its not pratical to move it to be the last script
    You don't have to put the EXITs in. I just threw that is as a freebie on one way to handle errors. It might be better to write to an error file if this is planned for automation.

    Let me know what you're trying to do completely and I can look over what you have and/or write a script for you. I just quickly threw some examples together before heading to work.

  5. #5
    Just Joined!
    Join Date
    Jul 2012
    Posts
    3
    Thats the complete script
    if i remove the exit 1 + exit 0
    doesnt it make the first part pointless
    and also the second part will error out if the folder doesnt exist which im trying to avoid


    sorry i cant post links yet
    Last edited by SarahH; 07-26-2012 at 02:15 AM.

  6. #6
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,497
    You will need to change the logic around a bit so that you only copy the files if the destination folder exists.

    Code:
    #add files if missing
    
    DEST="/usr/local/destination"
    SOURCE="/usr/local/source"
    
    if [ -d "$DEST" ]; then
        if [ ! -f "$DEST"/file1 ]; then
            cp "$SOURCE"/file1 "$DEST"
            cp "$SOURCE"/folder1/* "$DEST"/folder1/
            cp "$SOURCE"/folder2/file3 "$DEST"/folder2/
            cp "$SOURCE"/folder2/file4 "$DEST"/folder2/
            echo "files restored"
        fi
    fi
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

Posting Permissions

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