Showing posts with label if. Show all posts
Showing posts with label if. Show all posts

Saturday, 7 July 2012

Shell script - 3

Shell script - 3

To go to Session 1 click here
To go to Session 2 click here
To go to UNIX basic commands click here


Starting with “hello”
            The basic things for any program is input and output this can be achived using the two commands
1.      echo which similar to printf in c
2.      read which similar to scanf in c
First script
Example 7
            #!/bash/sh
            echo “hello”
            echo “Enter ur name”
            read nam
            echo “This is my first program $nam”
The Branching Statements
The next important thing in programming is branching to do a specific task based on specific condition.
1.      if .. fi
2.      if..else..fi
3.      if..elif..else..fi
4.      case..esac

if..fi syntax
if [ condition ]
then
            statements
fi
if..else..fi syntax
if [ condition ]
then
            statements
else
            statements
fi
if..elif..fi syntax
if [ condition ]
then
            statements
elif [ condition ]
            statements
else
            statements
fi

in these condition branching statements can be nested or there may be more than one elif based on requirements.
Example 7:
            #!/bin/sh
            filename=$1
if [-r filename] then
echo "file is readable"
elif [-x filename] then
echo "file is executable"
elif [-w filename] then
echo "file is writable"
else
echo "ERRROR: $filename HAS PERMISSION PROB"
fi
            case..esac syntax
            case variable in
                        pattern1)
                                    statements
                        ;;
                        pattern2)
                                    statements
                        ;;
                        ……
                        ……
                        patternn)
                                    statements
                        ;;
            esac
Example 8:
            #!/bin/sh
            name=$1
            case $name in
                        “jaya”)
                                    echo “Welcome buddy”
                                    ;;
                        “devi”)
                                    echo “Welcome sir”
                                    ;;
                        “Shiv”)
                                    echo “Welcome $name”
                                    ;;
            esac
Output
Testhome/l685360# ./test jaya
Welcome buddy
Testhome/l685360#
           
The looping

            The next important thing in programming is looping to do a specific task for some n times loops available in shell script are:

1.      while
2.      for
3.      unitil
4.      select

While Loop:

            Syntax:
                        While [ condition ]
                        do
                                    statements
                        done

Example 9:

#!/bin/sh
echo "Enter n "
read n
i=0
while [ $i -le $n ]
do
        echo "Welcome"
        i=`expr $i+1 `
done
Out Put:
            Testhome/l685360# ./test
Enter n
2
Welcome
Welcome
Welcome
Testhome/l685360#
*note : break can be used to break the loop
*note : continue can be used to continue the looping without executing the rest of lines.
            Example:
                        n=0
                        While [ $n < 10]
                        do
                                    if[ $n == 1]
                                    then
                                                continue;
                                    fi
                                    #if n==1 n becomes true it goes directly go to continue the loop without incrementing because of this, this loop will be endless loop
                                    n=`expr $n+1`
                        done


For loop:

Example 10:
#i!/bin/sh
n1="0 1 2 3 4 5 6 7 8 9 0 10"
for n in $n1
do
        echo $n
        echo "Welcome"
done

Out put:
Testhome/l685360# ./test
0
Welcome
1
Welcome
2
Welcome
3
Welcome
4
Welcome
5
Welcome
6
Welcome
7
Welcome
8
Welcome
9
Welcome
0
Welcome
10
Welcome
Testhome/l685360#

Until loop:

Until syntax:
 until  [ condition ]
 do
            statements
 done

Example 11:

#!/bin/sh
n = 10
x=0
until [ $x –lt $n ]
do
            echo $x
            x = `expr $x + 1`
done


Select Loop:

Select loop syntax:
select variable in word1 word2 …. Wordn
do
            statements
done

Example 12:

#!/bin/sh
select food in breakfast lunch dinner
do
        if [ $food == "breakfast" ]
        then
                echo "Good morning"
        elif [ $food == "lunch" ]
        then
                echo "Good After noon"
        elif [ $food == "dinner" ]
        then
                echo "Good night"
        else
                echo "Invalid option"
        fi
done
                       
Out put:
Testhome/l685360# ./test
1) breakfast
2) lunch
3) dinner
#? 1
Good morning
#? 2
Good After noon
#? 3
Good night
#?
Don't forget to put your feed back as comments which encourages me to do this and you can also post your questions or solve/give answers for others questions, Thanks in advance friends.....



To go to session 4 click here

Sunday, 19 February 2012

pl/sql best examples

Best links for mp3 and movie downloads

To learn UNIX basic commands click here
To learn Shell Script click here

Below is example code of pl/sql concepts
Addition of two numbers:

declare
 a number:=10;
 b number:=20;
 c number;
 begin
 c:=a+b;
 dbms_output.put_line(c);
 end;
 /

getting value from table and storing it in a variable and displaying it

declare
v_name varchar2(10);
begin
select ename into v_name from emp where empno=102;
 dbms_output.put_line('The emp name with emp no:102 is'|| v_name);
end;
/



Pay roll program in pl/sql
 declare
 eno number(5):=1245;
 ename varchar2(50):='jayachandran';
 ebasic number(7,2):=3000;
epf  number(7,2);
ehra number(7,2);
eda number(7,2);
ethome number(9,2);
begin
 epf:=0.02;
 epf:=epf*ebasic;
 ehra:=.12*ebasic;
 eda:=0.05*ebasic;
 ethome:=eda+ehra+ebasic-epf;
 dbms_output.put_line('Employee number ='||eno);
 dbms_output.put_line('Employee Name ='||ename);
 dbms_output.put_line('Employee take home ='||ethome);
 end;
/




 %type example in pl/sql

 declare
 v_name emp.ENAME%type;
begin
select ename into v_name from emp where empno=102;
 dbms_output.put_line('The emp name with emp no:102 is'|| v_name);
end;
/


if - else - if example in pl/sql
create table marks(name varchar2(10),math number(3),english number(3),tamil number(3));
declare
            stu marks.name%type;
            mat marks.math%type;
            eng marks.english%type;
            tam marks.tamil%type;
            tot marks.tamil%type;
            per number(5,2);
begin
            select  math,english,tamil into mat,eng,tam from marks where name = 'bala';
            tot:=mat+eng+tam;
            per:=((tot/300)*100);
            if(per = 100) then
                        dbms_output.put_line('you have scored s grade');
            else
                        if(per < 50) then
                                    dbms_output.put_line('you failed in exam');
            else
                        dbms_output.put_line('you passed exam');
            end if;
            end if;
end;
/


Go to session - 2