Writing your first script and getting it to work to successfully write a shell script, you have to do three things:
Write a script
Give the shell permission to execute it
Put it somewhere the shell can find it
Writing a script
A shell script is a file that contains ASCII text. To create a shell script, you use a text editor such as vim or nano. A text editor is a program, like a word processor, that reads and writes ASCII text files. There are many, many text editors available for your Linux system, both for the command line environment and the GUI environment.
Now, fire up your text editor
[you@yourllinuxbox you]$ nano yourfirstscript
and type in your first script as follows:
# My first script
echo "Hello World!"
The clever among you will have figured out how to copy and paste the text into your text editor. Then (^X) exit. Say (Y)es to save the modified buffer.
If you have ever opened a book on programming, you would immediately recognize this as the traditional "Hello World" program. Save your file with some descriptive name. How about yourfirstscript?
The first line of the script is important. This is a special clue given to the shell indicating what program is used to interpret the script. In this case, it is /bin/bash. Other scripting languages such as perl, awk, tcl, Tk, and python can also use this mechanism.
The second line is a comment. Everything that appears after a "#" symbol is ignored by bash. As your scripts become bigger and more complicated, comments become vital. They are used by programmers to explain what is going on so that others can figure it out. The last line is the echo command. This command simply prints what it is given on the display.
The next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows:
[you@yourllinuxbox you]$ chmod 755 yourfirstscript
The "755" will give you read, write, and execute permission. Everybody else will get only read and execute permission. If you want your script to be private (i.e., only you can read and execute), use "700" instead.
Putting it in your path
At this point, your script will run. Try this:
[you@yourllinuxbox you]$ ./yourfirstscript
You should see "Hello World!" displayed. If you do not, see what directory you really saved your script in, go there and try again.
Before we go any further, I have to stop and talk a while about paths. When you type in the name of a command, the system does not search the entire computer to find where the program is located. That would take a long time. You have noticed that you don't usually have to specify a complete path name to the program you want to run, the shell just seems to know.
Well, you are right. The shell does know. Here's how: the shell maintains a list of directories where executable files (programs) are kept, and just searches the directories in that list. If it does not find the program after searching each directory in the list, it will issue the famous command not found error message.
This list of directories is called your path. You can view the list of directories with the following command:
[you@yourllinuxbox you]$ echo $PATH
This will return a colon separated list of directories that will be searched if a specific path name is not given when a command is attempted. In our first attempt to execute your new script, we specified a pathname ("./") to the file.
You can add directories to your path with the following command, where directory is the name of the directory you want to add:
[you@yourllinuxbox you]$ export PATH=$PATH:directory
A better way would be to edit your .bash_profile file to include the above command. That way, it would be done automatically every time you log in. (Not really used anymore)
[you@yourllinuxbox you]$ PATH=$PATH:~/bin
[you@yourllinuxbox you]$ export PATH
Most modern Linux distributions encourage a practice in which each user has a specific directory for the programs he/she personally uses. This directory is called bin and is a subdirectory of your home directory (/home/you/). If you do not already have one, create it with the following command:
[you@yourllinuxbox you]$ mkdir bin
Move your script into your new bin directory and you're all set.
[you@yourllinuxbox you]$ mv yourfirstscript ~/bin/.
Now you just have to type:
[you@yourllinuxbox you]$ yourfirstscript
and your script will run.