(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



How and where to start writing first UNIX Shell Script

Sun Aug 29 13:08:30 2004 How and where to start writing first UNIX Shell Script
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

echo $0

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

cd directory_name

To get to know about your present working directory simply type

pwd

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

#!/bin/sh

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.




More Articles by Rakesh Awasthi




Click here to add your comments


---August 29, 2004

One small comment:

Every shell script must start with the following line

is untrue. It's not a bad idea, but it should be noted that your current shell will run a script without any such line, and that /bin/sh isn't necessarily the right choice.

--TonyLawrence




In this site i was looking for some Unix Shell scripts
for beginners and i didnt find any of them .
I think The site should include some examples of the
shell programing.

PRAVEENKUMAR.C.H


In this site i was looking for some Unix Shell scripts
for beginners and i didnt find any of them .
I think The site should include some examples of the
shell programing.

PRAVEENKUMAR.C.H

---October 1, 2004

OK. I don't LIKE shell programming, because shells are so pathetically bad at it, but maybe I or someone else can grit our teeth and throw a few out :-)

But there are sites really devoted to this: http://shelldorado.com/ for one..


--TonyLawrence






Fri Feb 25 20:50:22 2005: Subject: Bring the script to a close and back to the shell prompt   anonymous


I wrote a script that queries some files and redirects the output into a file, but the problem I am having is that the script never stops running. I have to hit CTRL C, CTRL C to exit back the the prompt.

Is there some command in the script that will bring me back to the prompt?



Fri Feb 25 23:06:23 2005: Subject: Command to bring you back to the prompt   TonyLawrence

gravatar

exit

Usually followed by 0 if it's a "good" exit:

exit 0

or a non-zero number if you want to indicate some error:

exit 1

That lets it be used like

yourprog || echo "it didn't work"

or

yourprog && echo it did work



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



/Blog/B1073.html copyright August 2004 Rakesh Awasthi All Rights Reserved

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server






More:
       - Programming
       - Shell
       - Basics
       - Blog


Unix/Linux Consultants

Skills Tests

Guest Post Here








card_image






My Favorites

Change Congress