UNIX App Question

Discussion in 'macOS' started by Benjamindaines, Feb 17, 2006.

  1. Benjamindaines macrumors 68030


    Mar 24, 2005
    A religiously oppressed state
    How do you write a UNIX executable that will run one line of code, then when that line is done it will run another and so on? I have the option to boot into Linux if needed.


    EDIT: After a bit of googleing, what I want to do is actually called a UNIX script.
  2. risc macrumors 68030


    Jul 23, 2004
    Melbourne, Australia
    If you are talking about writing a shell script you just use a text editor.

    If you want to try writing some shell scripts http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html has a nice guide on getting started using the Bash shell.

    You can then run the script by doing

    sh /path/to/script

    or make it executable by doing chmod +x script
  3. theBB macrumors 68020


    Jan 3, 2006
    Simple really... Just type up something along the lines of:

    echo "Hello world!..."
    echo "Second line"

    save it in a file, let's say, myfirst.sh

    The extension is not necessary at all, but seems to be the tradition. Now go the same directory where myfirst.sh is located through terminal. (If you don't know how to navigate to that directory by unix commands, you probably should learn a bit more about UNIX first.) Next step is to type:

    chmod 744 myfirst.sh

    which will make that script executable. (You can check it with

    ls -l myfirst.sh

    command. If you see something like -rwxr--r-- in the same line as myfirst.sh, you are good to go. The first "x" stands for executable by you.) Now, when you type


    at your command prompt, it will execute the script. However, if you don't include the directory where this script resides into your path, you can only run it when you are already inside that directory. Oh, the disclaimer: do all of these steps on your own risk (no warranties implied or otherwise.) This is just my personal opinion of how this could be done. :)
  4. Doctor Q Administrator

    Doctor Q

    Staff Member

    Sep 19, 2002
    Los Angeles
    You can even simplify what theBB suggests:

    1. type up something along the lines of:
    echo "Hello world!..."
    echo "Second line"​

    2. save it in a file, let's say, myfirst.sh

    3. run it:
    sh myfirst.sh​

    Explanation: By typing sh in front of the filename, you are tell sh (the shell) to execute the commands in that specific file (myfirst.sh). For this to work, file myfirst.sh need only be readable (which is already is), so you don't have to use the chmod command to make it executable.

Share This Page