Copying files with a Terminal Script to a Flash Drive

Discussion in 'Mac Programming' started by koolaid1551, Sep 27, 2010.

  1. macrumors newbie

    Joined:
    Sep 27, 2010
    #1
    Currently I am trying to have an executable script(script.term) that is located on my flash drive that i can run and it will copy files from my hard drive on my mac to my flash drive with no user interaction.

    My first problem is getting system variables to display

    The following command doesn't swap out the $HOME and $PWD environment variables:
    Code:
    cp -r $HOME/FolderToCopy $PWD/Results

    So i got the idea of trying to use the echo command in combination with the copy command following will just display the text and it will swap out the $HOME and $PWD variables with the values.
    Code:
    echo cp -r $HOME'/FolderToCopy' $PWD'/Results'
    Does anyone know how to run the copy command with variables in place.

    My second problem

    is the $PWD variable doesn't display the location of the flash drive as i thought it would. my second question was is there a variable to detect the location of where the file was executed.



    If it helps anyone here is a working solutions in windows command prompt of what i am trying to do
    Code:
    XCOPY C:\Users\%USERNAME%\FolderToCopy %CD%\Results /F /E /H
    Thanks
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    When prefaced with
    Code:
    #!/bin/bash
    and saved as test.sh this works just fine for me.

    B
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    Variables should be double-quoted. In case their expansion contains whitespace:
    Code:
    cp -r "$HOME/FolderToCopy" "$PWD/Results"
    
    Also, just because the script is located on your flash drive does not mean that $PWD will be the pathname of your flash drive. Maybe that's how it works on Windows (I'm guessing, since that's where I've seen that idiom), but it doesn't work that way on any Unix system.

    I suggest starting with a simpler script, one that simply echoes $HOME and $PWD. This should show you the reality of those values. You can work up from that simple case. Always best to start simple and work up, rather than guessing about how things work.

    When a script runs, "$0" will be the pathname of the script. You can echo it to see what it looks like. Look at the 'basename' and 'dirname' commands to help with parsing it. The command to read the man page for dirname is:
    Code:
    man dirname
    Or use Bwana:
    http://www.bruji.com/bwana/
     
  4. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    That could definitely be a part of the OPs problem. I was just trying to make sure that the script was invoking bash and shell substitution was "on" in its full glory.

    While it seems that in this case, koolaid1551 wants it to find the location of the script, the version using $PWD could be more general, depending on how it is executed.

    For example, the script using $PWD could be used from the flash drive to copy files over to a network share. From a shell prompt
    Code:
    cd /Volumes/Network
    /Volumes/Flash/test.sh
    
    would copy the files the /Volumes/Network/Results instead of /Volumes/Flash/Results

    The Windows for command offers many substitution modifiers documented here: and %~fI is similar to dirname($I). Wikipedia suggests the following alternative for dirname:

    So you could use ${0%/*}, dirname "$0" or $PWD depending on the intent.

    koolaid1551: Also look up backquotes. What you were trying to do with echo should make use of backquotes.

    B
     
  5. thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #5
    I got it to work i ended up just using the following.
    Code:
    cp -r "$HOME/FolderToCopy" /Volumes/Flash/
    I couldn't get the .sh to work even with the preface so i just used a term file

    But thanks for all your help
     
  6. macrumors 6502a

    Joined:
    Jan 15, 2007
    #6
    Did you make sure to set the script to be executable?

    ie

    chmod +x mysuperawesomescript.sh
     

Share This Page