Ways to execute a shell script

Discussion in 'Mac Apps and Mac App Store' started by Doctor Q, Nov 26, 2003.

  1. Doctor Q Administrator

    Doctor Q

    Staff Member

    Sep 19, 2002
    Los Angeles
    Suppose I create a shell script that does something useful. For example, it might make a backup copy of some important files. (The purpose of the script doesn't matter for my question.)

    My question: In what ways I can invoke this shell script on demand? I know a number of methods, but I'd like to know if there are more.

    --- Part 1: As a regular user ---

    I can invoke my shell script as follows:

    (1) Open a Terminal window and type the name of my shell script (assuming a suitable PATH and execute permission on the shell script).

    To use my shell script from the Finder by double-clicking something, I have these choices:

    (2) Name the shell script "something.command". Then, when I double click it, the Finder launches a Terminal window and executes the script.

    (3) Write an AppleScript that uses a "do shell script" command to run the shell script, and save the AppleScript as a double-clickable application.

    (4) Do the equivalent in another scripting language.

    (5) Use a real programming language to write a program that invokes my shell script (or, I suppose, the script from #3 or #4).

    Are there other ways?

    --- Part 2: As superuser ---

    Now let's add a complication. Suppose my shell script needs superuser (often called "root") privileges in order to do whatever it does. Now I have a slightly different set of chocies:

    (6) In Terminal, I can use the "sudo" command (typing "sudo" in front of the shell script name) to run my shell script.

    (7) In Terminal, I can use the "su" command to become superuser, and then run my shell script by typing its name.

    (8) I can log into the Finder as "root", so that I will already be superuser. Will all of methods #1 thru #5 then work without further prompts?

    (9) I can get the program named Skeleton Key and drag the file from any of methods #2 thru #5 into that application. It will prompt me for my root password and then execute the shell script. Is that correct? Are there other programs like Skeleton Key?

    Are there other ways to invoke my shell script when it needs superuser privileges?
  2. OutThere macrumors 603


    Dec 19, 2002
    Get "chron" to do it for you. Set it as a chron task and have it execute however often you want it to. (I think this will work)
  3. Doctor Q thread starter Administrator

    Doctor Q

    Staff Member

    Sep 19, 2002
    Los Angeles
    My question was only about "on demand" use of a shell script, but if we're going to include "automatic" ways too then yes, chron is one of them. I didn't check, but I assume that the "at" and "batch" commands can be used too.

    Another "automatic" way is to make one of these executables a startup item (either in /Users/myname/Library/StartupItems to be executed when I log in or in /System/Library/StartupItems to be executed at system boot time). The startup items used at system boot time are always run as superuser.
  4. daveL macrumors 68020


    Jun 18, 2003

    It's "cron", not "chron".

Share This Page