Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Benjamindaines

macrumors 68030
Original poster
Mar 24, 2005
2,841
5
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.

--Thanks.

EDIT: After a bit of googleing, what I want to do is actually called a UNIX script.
 
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. :)
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.