How do I create a .sh file to copy and replace a file?

Discussion in 'Mac Programming' started by connred, Feb 11, 2015.

  1. connred macrumors newbie

    Joined:
    Feb 11, 2015
    #1
    Hi,
    I do not know much and Im just getting into things. I am trying to create a .sh that can take a file, copy it, and then replace it with an existing file. I know how to do it in terminal, but I would to do it with a click of a button.
    Anyhelp? I know i'm not very experienced..
    Thanks
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Shell Scripts (.sh files) are normally entered on the command line to run them, not clicked with a button.

    When you're talking about something that only people familiar with the command line are using which doesn't benefit from a visual component, I'd say it's better to just leave it that way, too.

    Final thought: I'd recommend Python instead of Shell. Shell scripts only work on *nix systems whereas Python scripts will run anywhere, including but not limited to *nix and Windows.
     
  3. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #3
    I'd echo what ArtOfWarfare says, although I'd add that you can also call a shell script from an AppleScript which *can* run from a mouse click if you save it as an application. As an example,

    Code:
    set theDate to do shell script "date"
    However, if you're going to use AppleScript anyway then there's not really much point stepping out into "do shell script", you can move and delete files easy enough with pure AppleScript.
     
  4. JustMartin macrumors 6502a

    Joined:
    Feb 28, 2012
    Location:
    UK
    #4
    Lots of stuff online about writing shell scripts. In summary create a file

    On the first line indicate what shell you want to use - something like

    Code:
    #!/bin/sh
    follow that with the command(s) that you've been putting into terminal

    save the file and make it executable by the command

    Code:
    chmod +x filename
    That works if you want to issue the same command over and over again.
    If you want to use parameters, then you can refer to them in your script as $1, $2 etc. But, after that you're really getting into the realm of parameter checking, raising errors. All good stuff, but may be overkill for what you need
     
  5. DJLC macrumors 6502a

    DJLC

    Joined:
    Jul 17, 2005
    Location:
    Mooresville, NC
    #5
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
  7. Dargoth macrumors regular

    Dargoth

    Joined:
    Oct 27, 2014
    #7
    ...or just change the file extension from .sh to .command

    OS X will let you run it by double-clicking.
     

Share This Page