Shell Script Tutorial by Examples 1
July 10th, 2005

First lesson, what you script is what you were to type to terminal.(with some exception that you don’t need to care)
Shell scripting is a nice Unix(linux, OSX, unix etc.) feature. It allows us to use the command line interface to its full potential. In this article, I’ll attempt to to teach you shell scripting from the basic by giving out examples and learn from it.
In this article, I’ll assume that you are familiar with basic unix commands. I believe that without knowing these commands before you will be able to learn them through this tutorial. For freshening up,
| ls | list file, folder, etc. |
| cp | copy files |
| mv | move/rename file |
| cd | go to directory |
| mkdir | make directory |
| rmdir | remove directory |
While learning through this tutorial you will learn some new commands also, don’t worry too much if you don’t know some or all of the command in this list.
Let’s start with your first script, this script is mainly for learning purpose. So, it might look silly. Suppose you want to create two directories called first and second. If you are at the command prompt, here is the command you will type into the terminal(note $ denotes command prompt)
$mkdir first
$mkdir second
$mkdir first
$mkdir second
These commands will create two directories call first and second for you. Now, suppose you want to write a script that does that same thing as these two commands. First you will open your favorite text editor, xemacs for example, and create a file call myFirstScript.sh. And type the following text in(don’t forget the last line end with a new line character it’s a good practice to do so)
#!/bin/sh
mkdir third
mkdir fourth
#!/bin/sh
mkdir third
mkdir fourth
Now, save the script(myFirstScript.sh) and exit the editor. You can now run the script by using command called sh
$sh myFirstScript.sh
$sh myFirstScript.sh
If you issue ls command to the terminal you will find directories called third and fourth there. Yes, it’s that simple just type the command you were to type at the terminal to the script file an everything will just works*. *(with some exceptions that you don’t ever need to care)
But, calling sh myscript.sh is just uncool. we want to be able to do something like ./myScript.sh . So, let’s write another script to remove all the directories we just created. So, open your text editor and create a file calle removeDir.sh. And type the following in(again don’t forget the extra blank line) DON’T RUN IT YET
#!/bin/sh
rmdir first
rmdir second
rmdir third
rmdir fourth
#!/bin/sh
rmdir first
rmdir second
rmdir third
rmdir fourth
Now since we want to execute this script using command like ./myScript.sh . We need execute permission for it. So, we issue this command to the terminal
$chmod u+x removeDir.sh
$chmod u+x removeDir.sh
This command will give permission to execute file removeDir.sh. So, now we can try
$./removeDir.sh
$./removeDir.sh
You will find that director first, second, third and fourth are now gone.
In Summary, in this chapter you learned the most important basic for shell scripting that is what you script is what you were to type to terminal.
In the next chapter, I’ll cover variables and the traditional hello world script.
Entry Filed under: Uncategorized, Shell Script Tutorial
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed