First Script

Writing your first script and getting it to work

To successfully write a shell script, you have to do three things:

  1. Write a script
  2. Give the shell permission to execute it
  3. 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. 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. Here is a list of some common ones:

Name

Description

Interface

vi, vim

The granddaddy of Unix text editors, vi, is infamous for its difficult, non-intuitive command structure. On the bright side, vi is powerful, lightweight, and fast. Learning vi is a Unix rite of passage, since it is universally available on Unix/Linux systems. On most Linux distributions, an enhanced version of the traditional vi editor called vimis used.

command line

emacs

The true giant in the world of text editors is emacs by Richard Stallman. emacs contains (or can be made to contain) every feature ever conceived for a text editor. It should be noted that vi and emacs fans fight bitter religious wars over which is better.

command line

nano

nano is a free clone of the text editor supplied with the pine email program. nano is very easy to use but is very short on features. I recommend nano for first-time users who need a command line editor.

command line

gedit

gedit is the editor supplied with the Gnome desktop environment.

graphical

kwrite

kwrite is the "advanced editor" supplied with KDE. It has syntax highlighting, a helpful feature for programmers and script writers.

graphical

Now, fire up your text editor and type in your first script as follows:

#!/bin/bash
# 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

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 my_script?

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.

Setting permissions

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:

[me@linuxbox me]$ chmod 755 my_script

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:

[me@linuxbox me]$ ./my_script

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:

[me@linuxbox me]$ 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:

[me@linuxbox me]$ 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.

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. If you do not already have one, create it with the following command:

[me@linuxbox me]$ mkdir bin

Move your script into your new bin directory and you're all set. Now you just have to type:

[me@linuxbox me]$ my_script

and your script will run.

Editing the scripts you already have

Before we get to writing new scripts, I want to point out that you have some scripts of your own already. These scripts were put into your home directory when your account was created, and are used to configure the behavior of your sessions on the computer. You can edit these scripts to change things.

In this lesson, we will look at a couple of these scripts and learn a few important new concepts about the shell.

Commands, commands everywhere

Up to now, we really have not discussed exactly what commands are. Commands can be several different things. Some commands are built into the shell itself. That is, the shell automatically understands a few commands on its own. The commands cd and pwd are in this group. Commands implemented in the shell itself are called shell builtins. To see a list of the commands built into bash, use the help command.

The second type of commands is the executable programs. Most commands are in this group. Executable programs are all the files in the directories included in your path.

The last two groups of commands are contained in your runtime environment. During your session, the system is holding a number of facts about the world in its memory. This information is called the environment. The environment contains such things as your path, your user name, the name of the file where your mail is delivered, and much more. You can see a complete list of what is in your environment with the set command.

The two types of commands contained in the environment are aliases and shell functions.

Aliases

Now, before you become too confused about what I just said, let's make an alias. Make sure you are in your home directory. Using your favorite text editor, open the file .bash_profile and add this line to the end of the file:


alias l='ls -l'

The .bash_profile file is a shell script that is executed each time you log in. By adding the alias command to the file, we have created a new command called "l" which will perform "ls -l". To try out your new command, log out and log back in. Using this technique, you can create any number of custom commands for yourself. Here is another one for you to try:


alias today='date +"%A, %B %-d, %Y"'

This alias creates a new command called "today" that will display today's date with nice formatting.

By the way, the alias command is just another shell builtin. You can create your aliases directly at the command prompt; however they will only remain in effect during your current shell session. For example:

[me@linuxbox me]$ alias l='ls -l'

Shell functions

Aliases are good for very simple commands, but if you want to create something more complex, you should try shell functions. Shell functions can be thought of as "scripts within scripts" or little sub-scripts. Let's try one. Open .bash_profile with your text editor again and replace the alias for "today" with the following:


function today {
echo "Today's date is:"
date +"%A, %B %-d, %Y"
}

Believe it or not, function is a shell builtin too, and as with alias, you can enter shell functions directly at the command prompt.

[me@linuxbox me]$ function today {
> echo "Today's date is:"
> date +"%A, %B %-d, %Y"
> }
[me@linuxbox me]$

type

Since there are many types of commands, it can become confusing to tell what is an alias, a shell function or an executable file. To determine what a command is, use the type command. type will display what type of command it is. It can be used as follows:

[me@linuxbox me]$ type command

.bashrc

Though placing your aliases and shell functions in your .bash_profile will work, it is not considered good form. There is a separate file named .bashrc that is intended to be used for such things. You may notice a piece of code near the beginning of your .bash_profile that looks something like this:


if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

This script fragment checks to see if there is a .bashrc file in your home directory. If one is found, then the script will read its contents. If this code is in your .bash_profile, you should edit the .bashrc file and put your aliases and shell functions there.

 

Member Login| Logout
 
East African Standard Time

Username
Password
Showbiz - Soulja Boy Tell'em - "Kiss Me Thru The Phone"
 
Exclusive
 

Adventure
 
Adventure with us today. Find out more on travel agents and other facilities around East Africa from here. Find out the most fascinating places that you can not miss to visit while in East Africa. Get all this from richcomputersolutions - your favorite website. Remember if you want to join this website, suggest a user name and password and send them to richymany2k@gmail.com or click on sin-up for more. Joining this website is a 100% free. Remember that we offer the best in repair of computers, scanners , printers. On top of that, reach us for quality photography, internet connections .graphic designing, website designing, coomputer training and many mor services. You are welcome to the sole source of information in East Africa.
Advertise here
 
 
There have been 39246 visitors (169211 hits) To this Website today
I welcome you all to his website, for all the best you need and enjoy all the benefits as a member. This website website is still under construction but always hope to find more here with Rich Computer Solutions. Always come back for more events. This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free