Monday, July 21, 2008

Pattern Matching

Pattern Matching

Shell Scripts provide the regular expression to find and replace the patterns. Below are the few regular expressions used in the Linux bash.

  1. . (dot) Matches any one character Example:

"f." Matches ‘f’ followed by any single character "a.b" matches ‘a’ followed by any single character followed by ‘b’ Matching Meta character Use “\mata character” to match the meta character

Example: "a\.b" Matches a followed by dot ( and not any single character) followed by b

  1. [] ( squre bracket)

Example: [a b c] Matches single a, b or c [a - z] Matches single character from a to z

  1. * (asterisk) Matches 0 or more occurrence of the previous pattern Example:

“a*” Matches string having any number of ‘a’ character including zero. That means it also matches the string which do not contain the ‘a’.

  1. + Matches 1 or more occurrence of the following pattern Example: "a+" Matches string having any number of ‘a’ character

The only difference between the above one (*) and this is that this do not matches the strings that doesn’t contain the pattern preceding by ‘+’

  1. ^ Matches the pattern followed by it at the beginning of the line only Example: "^The" Matches the line start with “The” String Sample.txt The game is over Linux is the best operating system

egrep –i ‘\bThe’ Sample.txt will print the following line The game is over.

  1. $ Matches the pattern preceded by it at the end of the line only Example: "End$" Matches the line end with "end" String Sample.txt It’s End It’s End now

egrep –i ‘End$’ Sample.txt will print the following line It’s End

  1. \b ( word boundary ) Matches the pattern followed by it at the starting of the word. Example: "\bThe" Matches the word start with “The” String

Sample.txt The game is over Linux is the best operating system

egrep –i ‘\bT’ Sample.txt will print the following line

The game is over Linux is the best operating system

  1. { } curly bracket
    1. {x} Matches x or more occurrence of the preceding pattern

Example: “a{2}” matches string containing 2 or more occurrence of ‘a’.

Saturday, July 5, 2008

Shell Script Condition and Loops

If Else Condition

Case Statement

While loop

For Loop

1. IF Else Condition
Syntax:

if confidion ; then

#command to execute

elif condition then

#commands to execute

else

#commands to execute

fi

To check multiple conditions

If condition1 && condition2; then

#command to execute

elif condition 3 || condition4; then

#commands to execute

fi

if condition are generally used with the test command. Test command test the condition and return true if passed else it return false.

A=0

test a –gt 0 will return true.

There is one more thing similar to test. Square bracket.

[ a –gt 0 ] # remember there space after “[“ and before “]”

This is same as test a –gt 0.

Example:

Type.sh: this program will tell you whether you are pass, fail or distication based on you marks.


if [ $# -ne 1 ]; then # remember the space between if and condition echo "Usage: $0 Marks " exit 1 fi if [ $1 -le 35 ]; then echo "Fail" elif [ $1 -le 70 ]; then echo "PASS" elif [ $1 -le 100 ]; then echo "Distinction " else echo "invalid marks" fi


2. case statement

syntax:

case $variable-name in

pattern1)
command
….
….
Command;;

Pattern2)
command
….
.
command;;

pattern N)
command
….
….
command;;

pattern *) # if none of the above do not pattern matches

Command
….
….
command;;

esac # to end the case statement

Example:

Menu.sh Will execute the commands based on your choise.


echo –e “\t\t Menu “ echo -e “\n 1. List of file \n 2.list of all processes \n 3.User of system”

read choice

case “$choice” in

1) ls –l;;

2) ps –A;;

3) who;;

*) echo “invalid option”


3. While loop

Syntax:

While condition;

Do

# Commands to repeat

Done

Example:

Below program will print 1 to 10 numbers.

Count.sh


a=0 while[ a –lt 10 ]; do

expr a + 1

done


4. For Loop

Syntax:

For { Variable Name } in { list }

do # commands to repeat Done

Example:

Below program will do the same thing what above count.sh is doing


for i in 1 2 3 4 3 6 7 8 9 10; do

echo $i

done


Friday, July 4, 2008

Shell Script Computation and Comaprision

Comparison

Unlike other scripting language shell script has provided different methods to compare string and numbers.

1. Numeric Comparison Syntax : test Number1 option Number2 Test command will return the true of false based on the opration performed between two numbers Different options are as below:

· -ne means Not Equal to · -lt means Less Then · -le means Less Then or Equal to · -gt means Greater Then · -ge means Greater Then or Equal to

Example:


a=10 b=20

test $a –eq $b # will return False test $a –lt $b # will return True


  1. String Compression String comparison done with the simple mathematical operator Syntax: test string1 option string2 or test option string Different options are as below:

· = = means Equal to · != means Not Equal to · -n means Not NULL ( unary operator ) · -z means NULL ( unary operator )

Example:


Test “Gaurang” == “Gaurang” # this will return True A=”string “ Test -z “$A” # will Return false as A is not NULL


Note: Remember if you are compring strings and the strings are assigned in some variables you need to put that variables in the double quotes ( “$str” ), other wise it will give you error.

Computation

In the shell script you can’t directly add the two numbers as follows A=1 + 1 # this will gives you error

There is a special command called expr that is used for the normal integer computation. Syntax: expr num1 operator num2 Example:


a=2 b=3

c=`expr $a + $b` echo $c # will print 5, summation of 2 and 3

c= `expr $b / $a` echo $c #will print 1, as expr returns only integer value c= `expr $a * $b` # will gives you error, # asterisk ( * ) has some other special meaning

c=`expr $a \* $b` # correct way to multiply the value

echo $c # will print 6


Shell Script Basics

How to write the Shell Script ? You can use any of the available editors to write the shell script. Just write down the shell script save it as the filename.vi.

To execute the file use ./filename.sh at terminal window.

But before you execute the file you must have the execute permission of the file. So first write down the following command

chmod u+x filename

How to comment the line?

# is used to put the comment in the code

How to read the line? Read variable-name

How to print the line? Echo “Shell Script “ # will simple print the line Echo –e “\n Shell Script” # to use other formatters like “\n”, “\t” etc.. use echo –e Echo “$a”

How to assign the value to variable? a=0 # right way to assign the value to variable a= 0 # Error, Space after ‘=’ a =0 # Error, Space after ‘a’

Another thing Shell script is case sensitive. Means the no, No, nO and NO all are different variable and not the same.

How to assign the Value return by some command? You can’t directly assign the value to by some command to any variable as we done above.

a=expr 1 + 1 # this will gives you error

you needs to put the command in the `` ( quotes ). Like follows

a=`expr 1 + 1` #correct way echo $a # will print the 2

What is Command Line argument and how to pass it? Command line argument is nothing but the argument you pass with you program as a value or to change to flow of program.

For example when you use rm filename command. rm is the executable file which removes the other files. While filename is the arguments that shows which file to delete. Now you know why command line is used.

All the arguments you pass are stored in the special variables. Below are some of them

$1,$2….. Command line arguments you pass with program. $0 program. Name

$# Number of the arguments you passed

A sample program.

add.sh

if [ $# -ne 2 ]; then

echo “Usage: $0 Num1 Num2”

fi

expr $1 + $2 # adds the first and second arguments and print the sum.

Don’t worry if you are not getting the if condition and expr right now. We will cover it soon. To Execute the above program.

./add.sh 10 10

This will print the 20 as output.

Tuesday, July 1, 2008

Shell Scipt for beginners

Scripting languages are always easy and so Shell Script is too. But before you start the shell script there are few things that I need to tell you. If you are aware of the things you can skip the first topic.

What is shell Script ?

Shell Script basics

Shell Script Conditions and Loops

Shell Script Computation and Comparison

What is Shell Script ?

Shell script of nothing but the bunch of Linux command write down in the file with condition and loops. But this is little bit different form VB Script and the Java Script in a way that it is a bit of structured script. What mean that?? that means Spaces does matter in the script.

Almost all the Linux version provides following shells. You can easily change to any of the shell but BASH is the default shell on most of the Linux version.

sh or Bourne Shell: the original shell still used on UNIX systems and in UNIX related environments. This is the basic shell, a small program with few features. While this is not the standard shell, it is still available on every Linux system for compatibility with UNIX programs.

bash or Bourne Again shell: the standard GNU shell, intuitive and flexible. On Linux, bash is the standard shell for common users. This shell is a so-called superset of the Bourne shell, a set of add-ons and plug-in. This means that the Bourne Again shell is compatible with the Bourne shell: commands that work in sh, also work in bash. However, the reverse is not always the case

csh or C shell: the syntax of this shell resembles that of the C programming language.

tcsh or Turbo C shell: a superset of the common C shell, enhancing user friendliness and speed.

ksh or the Korn shell: sometimes appreciated by people with a UNIX background

To switch to any of the above shell just write down it’s name on the terminal window and press Enter