Thursday, April 14, 2011

Shell Scripting - part 2

Command Line Arguments
-------------------------------
Here we will discuss some more about command line arguments( values passed along with script like flags in commands)
and how they can be accessed in script

1)Name of the program
The name of the program is stored in variable '0'
So the value can be accessed as ${0}
e.g:- vi file1.sh
---------------------------------------------------------------------
#!/bin/bash
echo ${0}
---------------------------------------------------------------------
The output of the program will be like
./file.sh
2)All arguments passed along with script
All values passed to the script(command line arguments) will be stored in variable '@'
So the value can be accessed as ${@}
e.g:- vi file2.sh
---------------------------------------------------------------------
#!/bin/bash
echo ${0}
---------------------------------------------------------------------
If you execute the script with out any arguments,there will be no output
So to verify the script ,execute with some arguments like

root@localhost]#./file2.sh a b c

The output of the program will be like
a b c
3) Number of command line arguments
Number of arguments passed to the program will be stored in variable '#'
So the value can be accessed as ${#}
e.g:- vi file3.sh
---------------------------------------------------------------------
#!/bin/bash
echo ${#}
---------------------------------------------------------------------
If you execute the script with out any arguments,there will be no output
So to verify the script ,execute with some arguments like

root@localhost]#./file2.sh a b c

The output of the program will be like
3
4) PID of shell executing the script
variable that will store PID of shell executing will be on $ or _
So the value can be accessed as ${$} or ${_}
The PID of shell executing will depend on how you execute the program

The script can be executed in two way
a) on current shell
e.g:-
root@localhost]#. file3.sh
5388
b) on a separate shell
root@localhost]#./file3.sh
5344

On e.g (a) the PID displayed will be the PID of current shell
You can verify it using 'ps' command
root@localhost]#ps
PID TTY TIME CMD
5388 pts/0 00:00:00 bash
5464 pts/0 00:00:00 ps
5) Command line arguments
So finally we are on the actual arguments
The values that are passed to the script for processing

The values start with 1

e.g:- vi file4.sh
---------------------------------------------------------------------
#!/bin/bash
echo ${1}
echo ${2}
echo ${3}
---------------------------------------------------------------------
Execute the program with some arguments

root@localhost]#./file4.sh a b c
a b c

Thats for Now we will continue with more on coming posts

Tuesday, April 12, 2011

Shell Scripting - part 1

The echo command
------------------
The easiest command in shell script
used for display text

e.g:-
echo "Hello world"

Variables in script
--------------------
variables are used for storing values
It may be pe-defined or the value will be assigned on the fly (On execution of script)

The variable manipulation in scripting
VAR=value

${VAR} for retrieving the value assigned to VAR


a)Pre-defined
e.g:-
VAR=10

b)On the fly
e.g:-
VAR=`expr ${i} + 1`

In example (a) the variabe VAR is assigned a value of 10
The value of VAR will be 10 through out the execution life of script(unless that variable is not alterd by another assignment)

In example (b)the variable VAR assigned a value that depend on the value of 'i' (another variable)
We will split the example to understand two command

expr ${i} + 1
and
VAR=


i) evaluat the expression ${i} + 1
ii) Assign the result to VAR

Using the echo command we can display the value assigned to a variable

e.g:- Write a script that having a variable VAR and assign a value 10 to it
Display the value of VAR in terminal
---------------------------------------------------------------------

#!/bin/bash

#
# Dispaly the value of a variable

VAR=10
echo ${VAR}

---------------------------------------------------------------------

Monday, April 11, 2011

Basic Shell Scripting-step by step

Basic Shell Scripting-step by step

If you are not a beginner go to summery page

What is a script?

Group of commands in one file for a specific or a number of actions to achieve a result

Shell script structure

1. Select your editor

You must have a text editor to write shell programs

Select an editor in which you are comfortable

Most common editors are Vi and Emacs

2. Structure of a script

a)First line

“#!/path/to/shell”

b) Comments

Purpose of script, author, created date, modifications, bug fix notes etc

c) Body of script

Commands to be executed

This line will decide which shell to be used for executing the commands in script

If the line is not present, system will use default shell

E.g.:- 1

------------------------------------

#!/bin/bash

echo “Hello world”

E.g. Explained

------------------------------------

The above script will execute

echo “Hello world”

in bash shell

3. Execute a script

If you logged in as root, the home directory will be /root/

We will create a file in /root using vi editor

[root@localhost]# vi hello.sh

The files have an extension of “.sh” so that others can easily identify the file as script

-------------------------------------------------------------------------------------------------------------------------------

Note: - The “file” is a command used to identify, what kind of file it is.

Suppose we have file with name “abcd” How can I say it is a shell script or an executable binary or else another type..?

Here we will use “file” command

Eg:-

[root@localhost]# file abcd

ASCII Long line text

-------------------------------------------------------------------------------------------------------------------------------

We are in vi now (I’m going ahead with the assumption that you have basic knowledge of VI editor)

Type the text mentioned in e.g.: 1

#!/bin/bash

echo “Hello world”

Save the file

Now we have created a” bash shell script” named as “file.sh”

To execute a file that need execute permission

So give execute permission to “file.sh” using chmod command

[root@localhost]# chmod 755 file.sh


Now we can execute the script as

[root@localhost#]./file.sh

Hello world

[root@localhost#]

Summary

1. Create a script that will print “Hello world”

[root@localhost#]

[root@localhost#]

[root@localhost#]vi file.sh

#!/bin/bash

echo “Hello world”

[root@localhost#]chmod 755 file.sh

[root@localhost#]./file.sh

Hello world

[root@localhost#]

LVM thin provisioning - file system usage and pool usage dosn't match

When I was demonstrating LVM thin provisioning to new batch of campus hires ; they pointed out an important mismatch between thin pool usag...