UNIX App Question

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

  1. Benjamindaines macrumors 68030

    Benjamindaines

    Joined:
    Mar 24, 2005
    Location:
    A religiously oppressed state
    #1
    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.

    --Thanks.

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

    risc

    Joined:
    Jul 23, 2004
    Location:
    Melbourne, Australia
    #2
    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

    theBB

    Joined:
    Jan 3, 2006
    #3
    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

    myfirst.sh

    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

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #4
    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