Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

koolaid1551

macrumors newbie
Original poster
Sep 27, 2010
2
0
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
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/
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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
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:

... shell substitution is typically used instead
Code:
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

macrumors newbie
Original poster
Sep 27, 2010
2
0
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
 

foidulus

macrumors 6502a
Jan 15, 2007
904
1
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

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

ie

chmod +x mysuperawesomescript.sh
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.