osx - batch script

Discussion in 'Mac Programming' started by 537635, Nov 3, 2013.

  1. 537635 macrumors 6502

    Joined:
    Mar 7, 2009
    #1
    Not sure, whether this is the right forum, so move if necessary.

    I would like to create a batch script with rsync commands to perform regular semi-manual backups. I would like to start the script manually, which would then perform several rsync commands and I could see the terminal window as they were running.

    I tried with automator, which kinda works, but I can't see the results (they do what they're supposed to do, but with no feedback).

    Then I tried a shell script. Put a #!/bin/bash on a beginning of txt file and running it in terminal, but it doesn't work. What am I doing wrong?

    The script is just 3-4 rsync commands:

    rsync -va --delete /Users/........./Downloads/ /Volumes/backup......
    rsync -va --delete /Users/...../Dropbox/ /Volumes/backup......
    rsync -va --delete /Users/..../Library/Services/ /Volumes/backup.....
    rsync -va --delete /Users/...../Music/ /Volumes/backup.....

    Thanks in advance!
     
  2. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #2
    It sounds like you are almost there with your shell script.

    You should have a file, for example "copydata.txt" that contains (assuming that the lines of dots are filled in by actual pathnames):

    Code:
    #!/bin/bash
    
    rsync -va --delete /Users/........./Downloads/ /Volumes/backup......
    rsync -va --delete /Users/...../Dropbox/ /Volumes/backup......
    rsync -va --delete /Users/..../Library/Services/ /Volumes/backup.....
    rsync -va --delete /Users/...../Music/ /Volumes/backup.....
    
    It is more common to name shell scripts as something like "copydata.sh" but the filename is not important. I suspect that you have not made the file executable.

    Assuming that your file is named "copydata.txt" and is stored on the Desktop, execute the following commands in a Terminal window:

    Code:
    cd ~/Desktop
    chmod u+rwx copydata.txt
    
    You should now be able to run the command from a Terminal window using:

    Code:
    cd ~/Desktop
    ./copydata.txt
    
    If that works then right-clicking on the icon on the Desktop and selecting to open with the Terminal should execute the script. It looks like the OS X Finder defaults to using TextEdit to open any file with a name ending in ".txt" so you might need to switch to the more common ".sh" for clarity.

    If you want to run the script by double clicking the file, use a name that ends in ".command". For example, "copydata.command".
     
  3. 537635 thread starter macrumors 6502

    Joined:
    Mar 7, 2009
    #3
    And with much thanks to you I am finished with the script now. Thank you!

    I understand now about making the file executable. Just one more question - what is the purpuse of "./"?
     
  4. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #4
    The "." means the "current directory". The "/" is the character that separates directories when specifying a path. So "./copy data.sh" means the file named "copy data.sh" in the current directory.

    OS X and other Unix-like operating systems have an environment variable named "PATH" that defines the folders that should be searched to find executable files. If "." Is in the PATH variable then you would not need to specify "./" to run the script. I wanted to be explicit to avoid problems.

    You can view your PATH variable by running the following command in a Terminal:

    Code:
    echo "$PATH"
    
    You could also run your command in a new Terminal window by running:

    A.
    Code:
    Desktop/copydata.sh
    
    or

    B.
    Code:
    ~/Desktop/copydata.sh
    
    Example A does not change the current directory, so you need to specify that the command is in the Desktop directory. (A Terminal window opens with the current directory being the user's home directory.) in this example you can see that the current directory "." from earlier is just replaced by a directory name.

    In example B, the character "~" expands to the full path to current user's home directory. So no matter what directory is current, this example will run the script stored in your Desktop directory in your home directory.

    You can find more examples by searching the Internet for "absolute versus relative paths".
     
  5. 537635 thread starter macrumors 6502

    Joined:
    Mar 7, 2009

Share This Page