PDA

View Full Version : Need help w/ simple bash script




Super Macho Man
Sep 17, 2006, 04:25 PM
OK, I'm sure there is a simple solution to this, but I can't figure it out. This is a simple script to sync bookmarks and a couple other files between 2 computers. "cp" doesn't like the syntax and there are other errors as well. I am a Unix idiot.

#!/bin/bash

TARGETVOL="Macintosh HD-1"
TARGETUSER="alexd"

PATH_TO_SOURCE_BOOKMARKS=~/Library/Application\ Support/Camino
PATH_TO_SOURCE_DOCS=~/Documents.sparseimage

PATH_TO_TARGET_HOME="/Volumes/${TARGETVOL}/Users/${TARGETUSER}"
PATH_TO_TARGET_BOOKMARKS="${PATH_TO_TARGET_HOME}/Library/Application\ Support/Camino"
PATH_TO_TARGET_DESKTOP="${PATH_TO_TARGET_HOME}/Desktop"
PATH_TO_TARGET_DOCS=$PATH_TO_TARGET_HOME


echo "Backing up old bookmarks on target..."
cp "${PATH_TO_TARGET_BOOKMARKS}/bookmarks.plist" \
"${PATH_TO_TARGET_BOOKMARKS}/bookmarks.old.plist"

echo "Copying bookmarks from source to target..."
cp ${PATH_TO_SOURCE_BOOKMARKS}/bookmarks.plist $PATH_TO_TARGET_BOOKMARKS

echo "Copying desktop contents from source to target..."
cp ~/Desktop/* $PATH_TO_TARGET_DESKTOP

echo -n "Copy documents from source to target? (y/n) "
read -e $DOCSPROMPT

if [ $DOCSPROMPT = "y" ]; then
echo -n " Copying documents..."
cp $PATH_TO_SOURCE_DOCS $PATH_TO_TARGET_DOCS
echo "done"
fi

echo "Done"



Sayer
Sep 17, 2006, 05:10 PM
Maybe this:

echo "Backing up old bookmarks on target..."
cp "${PATH_TO_TARGET_BOOKMARKS}/bookmarks.plist" \
"${PATH_TO_TARGET_BOOKMARKS}/bookmarks.old.plist"

should be a move (mv) instead of copy (cp):

echo "Backing up old bookmarks on target..."
mv "${PATH_TO_TARGET_BOOKMARKS}/bookmarks.plist" \
"${PATH_TO_TARGET_BOOKMARKS}/bookmarks.old.plist"

If I read it correctly, using a copy will duplicate the file with a new name. So when you then try to copy the updated file over, you are hitting the old file, which is still there.

Using mv is typically how you rename a file in Unix.

After reading cp a few times you can use cp to replace an existing file if you use the -f flag in the command. And if you want to copy a directory and its contents (including sub-folders) use the -R flag on the cp command.

Super Macho Man
Sep 17, 2006, 05:17 PM
I've tried it with cp and mv, and the result either way is:

mv: rename /Volumes/Macintosh HD-1/Users/alexd/Library/Application\ Support/Camino/bookmarks.plist to /Volumes/Macintosh HD-1/Users/alexd/Library/Application\ Support/Camino/bookmarks.old.plist: No such file or directory

(or)
cp: /Volumes/Macintosh HD-1/Users/alexd/Library/Application\ Support/Camino/bookmarks.plist: No such file or directory

When I run the exact same command from the command line, it works - it's a valid path and bookmarks.plist does exist at that path. So it must be some problem with the script.

:confused:

MrFrankly
Sep 17, 2006, 05:23 PM
OK, I'm sure there is a simple solution to this, but I can't figure it out. This is a simple script to sync bookmarks and a couple other files between 2 computers. "cp" doesn't like the syntax and there are other errors as well. I am a Unix idiot.


cp probably doesn't like the fact that you have a space in your path (i.e Macintosh HD-1) which it will understand as a new argument. So use quotes " around the path names to make them as one, make sure you escape the properly.

Also like Sayer said, rename the file using mv. The only problem will be when you try to rename it for a second time and 'bookmarks.old.plist' is already there. It will still give an error message. For this you can use 'mv -f' to force the operation. It will override the target file without asking.

Super Macho Man
Sep 17, 2006, 06:39 PM
cp probably doesn't like the fact that you have a space in your path (i.e Macintosh HD-1) which it will understand as a new argument. So use quotes " around the path names to make them as one, make sure you escape the properly.

Also like Sayer said, rename the file using mv. The only problem will be when you try to rename it for a second time and 'bookmarks.old.plist' is already there. It will still give an error message. For this you can use 'mv -f' to force the operation. It will override the target file without asking.
I put quotes around Macintosh HD-1 where it was assigned to TARGETVOL (line 1). Is that what you mean? I also tried changing it to "Macintosh\ HD-1" - same result.

MrFrankly
Sep 18, 2006, 04:21 AM
try something like this (it might not work right away, it's more to show the use of double quotes " to allow a path with spaces):


TARGETVOL="Macintosh HD-1"
TARGETUSER="alexd"

PATH_TO_SOURCE_BOOKMARKS=~/Library/Application\ Support/Camino
PATH_TO_SOURCE_DOCS=~/Documents.sparseimage

PATH_TO_TARGET_HOME="/Volumes/${TARGETVOL}/Users/${TARGETUSER}"
PATH_TO_TARGET_BOOKMARKS="${PATH_TO_TARGET_HOME}/Library/Application Support/Camino"
PATH_TO_TARGET_DESKTOP="${PATH_TO_TARGET_HOME}/Desktop"
PATH_TO_TARGET_DOCS=$PATH_TO_TARGET_HOME


echo "Backing up old bookmarks on target..."
mv -f "\"${PATH_TO_TARGET_BOOKMARKS}/bookmarks.plist\"" \
"\"${PATH_TO_TARGET_BOOKMARKS}/bookmarks.old.plist\""

echo "Copying bookmarks from source to target..."
cp "\"${PATH_TO_SOURCE_BOOKMARKS}/bookmarks.plist\"" "\"$PATH_TO_TARGET_BOOKMARKS\""

echo "Copying desktop contents from source to target..."
cp ~/Desktop/* "\"$PATH_TO_TARGET_DESKTOP\""

echo -n "Copy documents from source to target? (y/n) "
read -e DOCSPROMPT

if [ "$DOCSPROMPT" = "y" ]; then
echo -n " Copying documents..."
cp "\"$PATH_TO_SOURCE_DOCS\"" "\"$PATH_TO_TARGET_DOCS\""
echo "done"
fi

echo "Done"


Not much didactic value now I know. I didn't test it, so you might still learn something from it if you have to debug. ;)

Super Macho Man
Sep 18, 2006, 07:00 PM
I fixed my script by changing the syntax a bit......... in other words, re-implementing it in AppleScript. :) It still needs work, but it's a little more readable now. ;)


set SourceCaminoFolder to ("iMac HD:Users:alexd:Library:Application Support:Camino")
set TargetCaminoFolder to ("PowerBook HD:Users:alexd:Library:Application Support:Camino")
set TargetCaminoPath to ("PowerBook HD:Users:alexd:Library:Application Support")
set SourceDesktopFolder to ("iMac HD:Users:alexd:Desktop")
set TargetDesktopPath to ("PowerBook HD:Users:alexd")
set SourceDocs to ("iMac HD:Users:alexd:Documents.sparseimage")
set TargetDocsFolder to ("PowerBook HD:Users:alexd")
set SourceMisc to ("iMac HD:Users:alexd:Misc.dmg")
set TargetMiscFolder to TargetDocsFolder

set DocsResponse to display dialog "Copy Documents.sparseimage from iMac to PowerBook?" buttons {"No", "Yes"} default button "No"
set MiscResponse to display dialog "Copy Misc.dmg from iMac to PowerBook?" buttons {"No", "Yes"} default button "No"

if button returned of DocsResponse = "Yes" then
tell application "Finder"
duplicate SourceDocs to TargetDocsFolder replacing yes
end tell
end if

if button returned of MiscResponse = "Yes" then
tell application "Finder"
duplicate SourceMisc to TargetMiscFolder replacing yes
end tell
end if

tell application "Finder"
duplicate TargetCaminoFolder replacing yes
end tell

tell application "Finder"
duplicate SourceCaminoFolder to TargetCaminoPath replacing yes
end tell

tell application "Finder"
duplicate SourceDesktopFolder to TargetDesktopPath replacing yes
end tell