Chat with us, powered by LiveChat
Shell Scripting

Shell Scripting

Hello Guys!
Are you intrested to write shell scripts and use it?.
Here are some basic instructions. Just follow it and you will be able to use shell script.
There are three main basic steps involved for writing it.

1. Write a script
2. Apply proper permission to execute it
3. place it on another path but the shell can find and execute it.

1.Writing a script

First of all try to understand what is shell script. A shell script is a file that contains ASCII text. You can use any editior to create a shell script. You can use a text editor. A text editor is a program, like a word processor, that reads and writes ASCII text files.
Now, open the vi text editor and type in your first script as follows:

#!/bin/bash
# My first script

echo “Hello Everybody!”

Save your file with some meaningful name. For example testscript.

Now Let us get understand the meaning. 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. They are used by programmers to explain step by step procedure, what is going on so that others can understand easily. The last line is the echo command. This command simply prints what it is given on the display.

2)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:

[[email protected]]# chmod 755 testscript

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:

[[email protected]]# ./testscript

You should see “Hello Everybody!” displayed. If you do not, see what directory you really saved your script in, go there and try again.

3)Place it on another path but the shell can find and execute it.

Before we go any further, Now let us understand about the paths. When you type in the name of a command, you don’t usually have to specify a complete path name to the program you want to run. The system does not search the entire filesystem to find where the program is located. That would take a long time. How it works? here paths come into play.

The shell does know about the path of commands. 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 command not found error message.

This list of directories is called your path. You can view the list of directories with the following command:
[[email protected]]# 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:
[[email protected]]# export PATH=$PATH:directory
You can include the above command to your .bash_profile file . That way, it would be done automatically every time you log in.

There is directory for each users to run the programmes called as bin directory. Most modern Linux distributions provide each user a specific directory for the programs he/she personally uses. If you do not already have one, create it with the following command:

[[email protected]]# mkdir bin

Move your script into your new bin directory. Now you just have to type:
[[email protected]]# testscript

and your script will run.

Sharing

Leave your comment

Pin It on Pinterest

Share This