PDA

View Full Version : Copying files with a Terminal Script to a Flash Drive




koolaid1551
Sep 27, 2010, 09:39 PM
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:
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.
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
XCOPY C:\Users\%USERNAME%\FolderToCopy %CD%\Results /F /E /H

Thanks



balamw
Sep 27, 2010, 10:45 PM
The following command doesn't swap out the $HOME and $PWD environment variables:
cp -r $HOME/FolderToCopy $PWD/Results

When prefaced with #!/bin/bash and saved as test.sh this works just fine for me.

B

chown33
Sep 28, 2010, 01:07 AM
Variables should be double-quoted. In case their expansion contains whitespace:
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:
man dirname
Or use Bwana:
http://www.bruji.com/bwana/

balamw
Sep 28, 2010, 07:40 AM
Variables should be double-quoted.


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.


Also, just because the script is located on your flash drive does not mean that $PWD will be the pathname of your flash drive.


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
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 (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true): and %~fI is similar to dirname($I). Wikipedia suggests the following alternative for dirname (http://en.wikipedia.org/wiki/Dirname):

... shell substitution is typically used instead
echo "${file%/*}";

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

koolaid1551
Oct 5, 2010, 10:01 PM
I got it to work i ended up just using the following.
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

foidulus
Oct 6, 2010, 03:58 AM
I got it to work i ended up just using the following.
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

Did you make sure to set the script to be executable?

ie

chmod +x mysuperawesomescript.sh