1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Need help w/ simple bash script

Discussion in 'Mac Programming' started by Super Macho Man, Sep 17, 2006.

  1. macrumors 6502a

    Super Macho Man

    #1
    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.

    Code:
    #!/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"
    
     
  2. macrumors 6502a

    Sayer

    #2
    Maybe this:

    Code:
    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):

    Code:
    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.
     
  3. macrumors 6502a

    Super Macho Man

    #3
    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:
     
  4. macrumors regular

    #4
    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.
     
  5. macrumors 6502a

    Super Macho Man

    #5
    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.
     
  6. macrumors regular

    #6
    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):

    Code:
    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. ;)
     
  7. macrumors 6502a

    Super Macho Man

    #7
    I fixed it!!!

    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. ;)

    Code:
    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
    
     

Share This Page