Posted by Rakesh Awasthi
If you are new to UNIX and having not much idea of UNIX, worried how to start writing shell scripts in UNIX, wondering why we need UNIX shell programming, etc. then this article will answer all your queries.
The article doesn't cover the shell scripting it only gives a brief idea about what is UNIX shell and how and where to start writing first shell script. I shall cover variable, expression, control structures, function, file handling, awk programming, debugging one-by-one in my subsequent articles on the UNIX shell scripting.
What is a UNIX shell?
I have worked on Windows 3.x/9x/2000/NT and even on XP, but I have never heard of a shell. What is it and why does every conversation on UNIX starts with the UNIX shell (of course security is also a hot topic)?
Those who have worked on any flavor of Windows, they would be familiar with the C:\>. It's a prompt, Command prompt. What it does for you? It allows you to enter some requests that you want from Operating System to perform.
In the same fashion the UNIX shell is a command interpreter that reads the command given by you as a user of the UNIX shell and executes them.
Which shell am I using?
Why is this question first of all? I just logged into my UNIX system and started working and now I want to write my first shell script. Is there any other shell available than the one I am working in?
Guess how many UNIX shell are available. There are more than 30. Though sh, bash, csh, ksh, tcsh are the shell that you will find more often. These shells are convenient to use to you as a user and as a programmer.
These shells are different by each other in the user privileges, commands and in providing different interactive features.
To know which shell you are using type
on the shell prompt string.
Why should I use shell for programming?
I am very good in C and/or Perl and can write hundreds of lines of code in a day? Then why do I need to write my programs or scripts in the shell scripts?
Yea, it's very true that era is to use graphical interfaces. No doubt. But for the simple file manipulation, automating tasks, writing system administration tasks writing a shell script is quickest and simplest bet.
Where to write my first UNIX shell script?
Okay, I am becoming a little bit impatient and want to start with my first shell script. I have just logged in. where am I? Where should I write my first shell script?
The directory where you find yourself immediately after doing login is your home directory. To change the directory type this on the shell prompt
To get to know about your present working directory simply type
You can start writing your shell scripts in any directory. You would be able to execute your script by giving the respective path to it from other directory also.
Creating first shell script
You can start writing your first shell script program at the prompt string itself. But that won't be available to you after its execution. Moreover, you will not be able to debug your program in case you find errors (that is not very surprising at early stage). So I would suggest you to use some kind of text editor. vi is the most common used editor for this purpose.
What should be my first line?
Every shell script must start with the following line
The line is called a shebang line due to first two characters(#!) in the line. # is sometimes called as sharp and ! as bang. This tells the UNIX to execute the script with the correct interpreter.
Classic "Hello World!" Program
Let's start with the classic "Hello World" program. To get the desired output type
echo "Hello World!" save file and try to run it using ./first
Why is my script not running?
What went wrong? I did the same that you have said and it's a one line script only.
Don't worry. Nothing is wrong with your shell script. The file is not an executable file. You need to make it executable by writing simple command.
chmod +x first
Now try doing ./first you will get the expected output.
How to comment something?
I am the only one who is going to use this script. Why should I bother to write comments?
This may be true, but after writing many shell scripts and then coming back to your first one? Believe me, sometimes it would not be easy for you to understand your own program. It's a good practice to put comments for the better understanding of the program.
Got something to add? Send me email.
More Articles by Rakesh Awasthi © 2011-03-09 Rakesh Awasthi
I always knew that one day Smalltalk would replace Java. I just didn't know it would be called Ruby. (Kent Beck)