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

    Feb 11, 2015
    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..
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    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


    Feb 12, 2008
    East Riding of Yorkshire, UK
    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,

    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

    Feb 28, 2012
    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

    follow that with the command(s) that you've been putting into terminal

    save the file and make it executable by the command

    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


    Jul 17, 2005
    Mooresville, NC
  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
  7. Dargoth macrumors regular


    Oct 27, 2014
    ...or just change the file extension from .sh to .command

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

Share This Page