11 basic shell programs enough for ignou exams observe syntax

Upload: vishal-falake

Post on 06-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 11 Basic Shell Programs Enough for IGNOU Exams Observe Syntax

    1/7

    Program 1

    WAP to concatenate the contents of 2 files ?

    cat > first

    hai this is first file^d

    cat > second

    hai this is Second file

    ^d

    program

    -------

    vi prog1echo contents of first file

    cat first

    echo contents of second file

    cat second

    echo contents of third file

    cat first second > third

    cat third

    esc : x

    output

    ------

    sh prog1

    Program 2

    WAP to Find SUm,AVG,PRODUCT of 4 numbers ?

    vi prog2

    clearecho enter A,B,C,D values

    read a

    read b

    read c

    read d

    sum= `Expr $a + $b + $c + $d`

    Avg= `Expr $sum /4`

    Product= `Expr $a \* $b \* $c \*d`

  • 8/3/2019 11 Basic Shell Programs Enough for IGNOU Exams Observe Syntax

    2/7

    echo Sum is $sum

    echo Avg is $Avg

    echo Product is $ Product

    esc:x

    output

    ------

    sh prog2

    Program 3

    WAP to change two values with out using another variable ?

    vi prog3

    clearecho enter A,B values

    read a

    read b

    echo actual values are A="$a" and B="$b"

    a=`Expr $a + $b`

    b=`Expr $a - $b`

    a=`Expr $a - $b`

    echo swapped values are "A"=$a and "B"=$b

    esc:x

    output

    -----

    sh prog3

    Program 4

    WAP to find largest of 3 numbers

    vi prog4

    clear

    read a

    read b

    read c

    if [$a -eq $b -a $b -eq $c

    then

    echo 3 numbers are equal

  • 8/3/2019 11 Basic Shell Programs Enough for IGNOU Exams Observe Syntax

    3/7

    elif [$a -gt $b -a $a -gt $c]

    then echo $a is Bigger

    elif [$b -gt $c]

    then echo $b is Bigger

    elseecho $c is Bigger

    fi

    output

    ------

    sh prog4

    Program 5

    WAp to find the Factorial of a given number

    vi prog5

    clear

    echo enter the number to get Factorial

    read n

    fact=1

    i=$n

    while [$n -ge 1]do

    fact =`Expr $fact \* $n`

    n=`Expr $n-1`

    done

    echo Factorial of $i is $fact

    output

    ------

    sh prog5

    Program 6

    WAP to find the Sum of INDIVIDUAL digits of given numbers

    vi prog6

    clear

    echo enter any number having atleast 2 digits

    read ni=$n

  • 8/3/2019 11 Basic Shell Programs Enough for IGNOU Exams Observe Syntax

    4/7

    r=0

    sum=0

    while test $n -gt 0

    do

    r=`Expr $n %10`sum=`Expr $sum + $r`

    n=`Expr $n/10`

    done

    echo sum of individual digits of $i is $sum

    output

    -------

    sh prog6

    Program 7

    WAP to find the reverse of a given number

    vi prog7

    clear

    echo enter a number

    read n

    m=$n

    x=0

    while test $n -gt 0

    do

    r=`Expr $n %10`

    x=`Expr $x \*10 + $r`

    n=`Expr $n/10`

    done

    echo reverse of given number is $x

    output

    ------

    sh prog7

    Program 8

    WAP to generate fibnacci series

    vi prog8

    clear

    echo enter any number

    read n

  • 8/3/2019 11 Basic Shell Programs Enough for IGNOU Exams Observe Syntax

    5/7

    fo=0

    f1=1

    echo $fo

    echo $f1

    i=2

    while test $i le $n

    do

    f2=`Expr $fo + $f1`

    echo $f2

    f0=$f1

    f1=$f2

    i=`Expr $i+1`

    done

    output

    ------

    sh prog8

    Program 9

    WAP to check whether a given number is PALLENDROME or NOT

    vi prog9

    echo enter any number

    read n

    x=0

    m=$n

    while test $n -gt 0

    do

    r=`Expr $n%10`

    x=`Expr $x \*10 +r`

    n=`Expr $n/10`

    done

    if test $x -eq $m

    then

    echo given number is PALLENDROME

    else

    echo given number is NOT a PALLENDROME

    fi

    output

    ------

    sh prog9

  • 8/3/2019 11 Basic Shell Programs Enough for IGNOU Exams Observe Syntax

    6/7

    Program 10

    Write a menu Driven Prog

    vi prog10

    clearecho "1.Contents of a FILE "

    echo

    echo "2.Present Working Directory"

    echo

    echo "3.EXIT"

    echo

    echo "Enter your Choice"

    read ch

    case $ch in1)echo enter file name

    read filename

    cat $filename

    ;;

    2)echo present working directory

    pwd

    ;;

    3)echo program EXIT

    ;;

    *)echo Incorrect choice;;

    esac

    output

    -------

    sh prog10

    Program 11

    WAP to count the no.of UPPER case letters , no.of lowercase letters, no.of digits

    and no.of DIGITS and no.of Special characters in a given string

    vi prog11

    clear

    echo enter a string

    read s

    len=`echo $s |wc-c`

    h=`echo $len -1`

    echo length is $h

    u=0

  • 8/3/2019 11 Basic Shell Programs Enough for IGNOU Exams Observe Syntax

    7/7

    l=0

    d=0

    sc=0

    while test $n -gt 0

    doa=`echo $s |cat-c $n`

    case $a in

    [a-z])

    l=`Expr $l+1`

    ;;

    [A-Z])

    u=`Expr $u+1`

    ;;

    [0-9])

    d=`Expr $d+1`

    ;;

    *)

    sc=`Expr $sc+1`

    ;;

    esac

    n=`Expr $n-1`done

    echo no.of uppercase letters are $u

    echo no.of lowercase letters are $l

    echo no.of digits are $d

    echo no.of special characters are $sc