Os Final Ritesh

  • Upload
    rubina

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

  • 8/8/2019 Os Final Ritesh

    1/32

    qwertyuiopasdfghjklzxcvbnmqwerty

    uiopasdfghjklzxcvbnmqwertyuiopasd

    fghjklzxcvbnmqwertyuiopasdfghjklzx

    cvbnmqwertyuiopasdfghjklzxcvbnmq

    wertyuiopasdfghjklzxcvbnmqwertyui

    opasdfghjklzxcvbnmqwertyuiopasdfg

    hjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmq

    wertyuiopasdfghjklzxcvbnmqwertyui

    opasdfghjklzxcvbnmqwertyuiopasdfg

    hjklzxcvbnmqwertyuiopasdfghjklzxc

    vbnmqwertyuiopasdfghjklzxcvbnmq

    wertyuiopasdfghjklzxcvbnmqwertyui

    opasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmrtyuiopasdfghjklzxcvbn

    mqwertyuiopasdfghjklzxcvbnmqwert

    yuiopasdfghjklzxcvbnmqwertyuiopas

    K.P.Patel School of Management &Computer Studies (KSMCS)

    Programming Skills-V(OS)

    MCA

    Raish Vhora

  • 8/8/2019 Os Final Ritesh

    2/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    2 Raish Vhora

    1. Check the output of the following commands.

    date, ls, who, cal, ps, wc, cat, uname, pwd, mkdir, rmdir, cd, cp, rm, mv, diff, chmod,

    grep, sed, head, tail, cut, paste, sort, find.

    1) datedate - print or set the system date and timeOUTPUT

    digit@ubuntu:~$ date

    Thu Dec 9 00:14:12 EST 2010

    2) lsls - list directory contents

    OUTPUT

    digit@ubuntu:~$ ls

    Desktop Downloads Firefox_wallpaper.png Pictures Templates VLC

    Documents examples.desktop Music

    3) whowho - show who is logged on

    OUTPUT

    digit@ubuntu:~$ man who

    who

    who

    digit tty7 2010-12-08 23:36 (:0)

    digit pts/0 2010 -12-09 00:12 (:0.0)

    4) calcal, ncal displays a calendar and the date of Easter

    OUTPUIT

    digit@ubuntu:~$ cal

    December 2010

    Su Mo Tu We Th Fr Sa

    1 2 3 4

    5 6 7 8 9 10 11

    12 13 14 15 16 17 18

    19 20 21 22 23 24 2526 27 28 29 30 31

    5) psps - report a snapshot of the current processes.

    OUTPUT

    digit@ubuntu:~$ ps

  • 8/8/2019 Os Final Ritesh

    3/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    3 Raish Vhora

    PID TTY TIME CMD

    3008 pts/0 00:00:00 bash

    3025 pts/0 00:00:00 man

    3035 pts/0 00:00:00 pager

    3041 pts/0 00:00:00 man

    3051 pts/0 00:00:00 pager

    3216 pts/0 00:00:00 man

    3226 pts/0 00:00:00 pager

    3236 pts/0 00:00:00 man

    3246 pts/0 00:00:00 pager

    3256 pts/0 00:00:00 man

    3266 pts/0 00:00:00 pager

    3276 pts/0 00:00:00 man

    3285 pts/0 00:00:00 pager

    3292 pts/0 00:00:00 ps

    6) wcwc - print newline, word, and byte counts for each file

    digit@ubuntu:~/VLC$ wc install.sh

    33 89 603 install.sh

    7) catcat - concatenate files and print on the standard output

    digit@ubuntu:~/VLC$ cat f1.txt

    Nadiad

    Anand

    Baroda

    8) unameuname - print system information

    digit@ ubuntu:~/VLC$ uname

    Linux

    9) pwdpwd - print name of current/working directory

    digit@ubuntu:~/VLC$ pwd

    /home/digit/VLC

    10) mkdirmkdir - make directories

  • 8/8/2019 Os Final Ritesh

    4/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    4 Raish Vhora

    digit@ubuntu:~/VLC$ mkdir KSMCS

    11) rmdirrmdir - remove empty directories

    digit@ubuntu:~/VLC$ rmdir KSMCS

    12) cdcd to change the current directory

    digit@ubuntu:~/VLC$ cd KSMCS

    13) cpcp - copy files and directories

    digit@ubuntu:~/VLC$ cp f1.txt ff.txt

    14) rmrm - remove files or directories

    digit@ubuntu:~/VLC$ rm KSMCS

    15) mvmv - move (rename) files

    mv KSMCS MCA

    16) diffdiff - compare files line by linedigit@ubuntu:~/VLC$ diff f1.txt f2.txt

    17) chmodchmod - change file mode bits

    digit@ubuntu:~/VLC$ chmod +ux prg1.sh

    18) grepgrep, egrep, fgrep, rgrep - print lines matching a pattern

    grep -c $i temp

    19) sedsed - stream editor for filtering and transforming text

    sed -n "$eno"'!p' $fnm > te.lst

    20) headhead - output the first part of files

  • 8/8/2019 Os Final Ritesh

    5/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    5 Raish Vhora

    digit@ubuntu:~/VLC$ head f1.txt

    Nadiad

    Anand

    Baroda

    21) tailtail - output the last part of files

    digit@ubuntu:~/VLC$ tail f1.txt

    Nadiad

    Anand

    Baroda

    22) cutcut - remove sections from each line of files

    cut -d ' ' -f1

    23) pastepaste - merge lines of files

    paste f1.txt f2.txt

    24) sortsort - sort lines of text files

    digit@ubuntu:~/VLC$ sort f1.txt

    Anand

    Baroda

    Nadiad

    25) findfind - search for files in a directory hierarchy

    digit@ubuntu:~/VLC$ find f1.txt ff

    2. Write shell script

    a) Accept numbers and perform addition, subtraction, division and ultiplication. echo "Enter a First number:"

    read a

    echo "Entre a Second number:"

    read b

    echo "+"

    echo "-"

    echo "*"

  • 8/8/2019 Os Final Ritesh

    6/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    6 Raish Vhora

    echo "/"

    echo "Enter a choice:"

    read ch

    if [ "$ch" = "+" ];

    then

    echo "$a + $b =" `expr $a + $b` ;

    elif [ "$ch" = "-" ];

    then

    echo "$a - $b =" `expr $a - $b` ;

    elif [ "$ch" = "*" ];

    then

    echo "$a * $b =" `expr $a \* $b` ;

    elif [ "$ch" = "/" ];

    then

    echo "$a / $b =" `expr $a / $b` ;

    fib) Accept the string and checks whether the string is palindrome or not.

    clear

    echo "enter string:- \c"

    read s

    l=${#s}

    echo "length of string is:- "$l

    rev=""

    while [ $l -ge 1 ]

    doch=`echo $s | cut -c$l`

    rev=$rev$ch

    l=`expr $l - 1`

    done

    echo "reverse is" $rev

    if [ $rev = $s ]

    then

    echo "palindrom"

    elseecho "not palindrom"

    fic) Accept number and check the number is even or odd, finds the length of the

    number, sum of the digitsin the number.

    clear

    echo "Enter The Number :\c"

    read n

  • 8/8/2019 Os Final Ritesh

    7/32

  • 8/8/2019 Os Final Ritesh

    8/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    8 Raish Vhora

    then

    t=`ls -l $name | tr -s ' ' | cut -d ' ' -f6,7`

    echo Modification time of file is $t

    else

    echo "Does Not Exit"

    fif) Fetch the data from a file and display data into another file in reverse order.

    clear

    echo "\n Enter The Directory Name : \c"

    read dname

    l=`cat $dname | wc -l`

    rm reverse

    while [ $l -gt 1 ]

    dostr=`head -$l $dname | tail -l`

    echo $str > temp

    rev=`rev temp`

    echo $rev >> reverse

    l=`expr $l -l`

    done

    cat reverse

    3. Write a script to find the global complete path for any file. clear

    echo "enter file name:- "

    read fname

    echo "file path is:- "

    find / -name $fname

    4. Write a script to broadcast a message to a specified user or a group of users

    logged on any terminal.

    clear

    echo "========================="

    echo "W R I T E - M E S S A G E"

    echo "========================="

    echo "[1] Create Message "

    echo "[2] To Single User"

    echo "[3] To All User"

  • 8/8/2019 Os Final Ritesh

    9/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    9 Raish Vhora

    echo "[4] Exit"

    echo "========================="

    echo "Enter Your Choice:- \c"

    read ch

    case $ch in

    1) echo "Write Message [Press Ctrl+d to terminate]"

    cat > message

    ;;

    2) echo "Enter User Name:- \c"

    read uname

    write $uname < message

    ;;

    3)

    who | cut -d ' ' -f1 | uniq > temp

    set cat temp`

    for i in $*

    do

    write $i < message

    done

    rm temp

    ;;

    4)

    exit

    ;;

    esac5. Write a script to copy the file system from two directories to a new directory in

    such a way that only the latest file is copied in case there are common files in both

    the directories.

    clear

    echo "enter source directory 1 :- \c"

    read dir1

    if [ ! -d $dir1 ]

    then

    echo "$dir1 directory does not exists"exit

    fi

    echo "enter source directory 2 :- \c"

    read dir2

    if [ ! -d $dir2 ]

  • 8/8/2019 Os Final Ritesh

    10/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    10 Raish Vhora

    then

    echo "$dir2 directory does not exists"

    exit

    fi

    echo "enter destination directory 3:- \c"

    read dir3

    if [ ! -d $dir3 ]

    then

    echo "$dir3 directory does not exists"

    exit

    fi

    set `ls $dir1`

    `ls $dir2 > temp`

    for i in $*

    do

    c=`grep -c $i temp`

    if [ $c -eq 0 ]

    then

    cp "$dir1/$i" $dir3

    else

    if [ "$dir1/$i" -nt "$dir2/$i" ]

    then

    echo "$dir1/$i copied to $dir3"

    cp "$dir1/$i" $dir3

    else

    echo "$dir2/$i copied to $dir3"

    cp "$dir2/$i" $dir3

    fi

    fi

    done

    6. Write a script to compare identically named files in two different directoriesand if they are same, copy one of them in a third directory.

    clear

    echo "enter source directory 1 :- \c"

    read dir1

    if [ ! -d $dir1 ]

    then

  • 8/8/2019 Os Final Ritesh

    11/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    11 Raish Vhora

    echo "$dir1 directory does not exists"

    exit

    fi

    echo "enter source directory 2 :- \c"

    read dir2

    if [ ! -d $dir2 ]

    then

    echo "$dir2 directory does not exists"

    exit

    fi

    echo "enter destination directory 3:- \c"

    read dir3

    if [ ! -d $dir3 ]

    then

    echo "$dir3 directory does not exists"

    exit

    fi

    set ` ls $dir1 `

    ` ls $dir2 > temp `

    for i in $*

    do

    c=` grep -c $i temp `

    if [ $c -eq 1 ]

    then

    c=` cmp "$dir1/$i" "$dir2/$i" | wc -l `

    if [ $c -eq 0 ]

    then

    echo "$dir1/$i copied to $dir3"

    cp "$dir1/$i" $dir3

    fi

    fi

    done7. Write a script to delete zero sized files from a given directory (and all its sub -

    directories).

    $ find . -empty -maxdepth 1 -exec rm {} \;

    to find zero size file

  • 8/8/2019 Os Final Ritesh

    12/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    12 Raish Vhora

    find . -size 0c

    8. Write a script to display the name of those files (in the given directory) which

    are having multiple links.

    clearecho "enter directory name:- \c"

    read dname

    if [ -d $dname ]

    then

    `ls -l $dname | tr -s ' ' | cut -d ' ' -f2,8 > temp`

    l=`cat temp | wc -l`

    i=2

    while [ $i -le $l ]

    dostr=`head -$i temp | tail -l`

    link=`echo $str | cut -d ' ' -f1`

    fname=`echo $str | cut -d ' ' -f2`

    if [ $link -gt 1 ]

    then

    echo "file $fname has $link link"

    fi

    i=`expr $i + 1`

    done

    elseecho "not exist"

    fi

    9. Write a script to display the name of all executable files in the given directory.

    clear

    echo "enter directory home:- \c"

    read dname

    if [ -d $dname ]

    then set `ls $dname`

    for i in $*

    do

    if [ -x $i ]

    then

    echo $i

  • 8/8/2019 Os Final Ritesh

    13/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    13 Raish Vhora

    fi

    done

    else

    echo "file does not exist"

    fi

    or

    clear

    echo "\n Enter The Directory :\c"

    read dname

    if [ -d $dname ]

    then

    ls $dname

    for i in $*

    do

    if [ -x $i ]

    then

    echo $i

    ls $i

    fi

    done

    else

    echo "\n Does Not Exits"

    fi

    10.Write a script to display the date, time and a welcome message (like Good

    Morning etc.). The time should be displayed with a.m. or p.m. and not in 24

    hours notation.

    month=`date +%B`

    day=`date +%d`year=`date +%Y`

    hour=`date +%k`

    minutes=`date +%M`

    if [ $hour -gt 12 ]

    then

    var="p.m"

    else

  • 8/8/2019 Os Final Ritesh

    14/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    14 Raish Vhora

    var="a.m"

    fi

    if [ $hour -gt 0 -a $hour -lt 12 ]

    then

    echo "Good Morning"

    elif [ $hour -ge 12 -a $hour -lt 16 ]

    then

    echo "Good Afternoon"

    elif [ $hour -ge 16 -a $hour -lt 19 ]

    then

    echo "Good Evening"

    else

    echo "Good Night"

    fi

    if [ $hour -gt 12 ]

    then

    hour=`expr $hour - 12`

    fi

    echo "Date is "$day-$month-$year

    echo "Time is "$hour : $minutes $var

    11. Write a script to display the directory in the descending order of the size of

    each file.

    clear

    echo "enter directory:- \c"

    read dname

    if [ -d $name ]

    then

    echo "file in descending order"

    echo "size filename"

    `ls -l $dname | tr -s ' ' | cut -d ' ' -f5,8 > temp`

    sort -grk1 temp

    elseecho "directory does not exist"

    fi

    12. Write a script to implement the following commands:

    Tree (of DOS) which (of UNIX)

    echo "______________use tree _____________ "

    tree -d | head -20

  • 8/8/2019 Os Final Ritesh

    15/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    15 Raish Vhora

    echo "_______________use which_____________"

    echo "ENTER THE COMMAND WE CAN FIND PATH"

    read a

    which $a

    13. Write a script for generating a mark sheet after reading data from a file.

    File contains student roll no, name , marks of three subjects.

    if [ $# -ne 3 ]

    then

    echo "You have to Enter 3 arguments for marks....."

    else

    m1=$1

    m2=$2

    m3=$3

    sum=`expr $m1 + $m2 + $m3`

    avg=`expr $sum / 3`

    echo "\nTotal is --> "$sum

    echo "Average marks are --> " $avg

    if [ $avg -ge 70 ]

    then

    echo "\n\nStudent has achieved Distinction..."

    elif [ $avg -ge 60 and -lt 70 ]

    then

    echo "\n\nStudent has achieved First class..."elif [ $avg -ge 50 and -lt 60 ]

    then

    echo "\n\nStudent has achieved Second class..."

    elif [ $avg -ge 40 and -lt 50 ]

    then

    echo "\n\nStudent has achieved Third class..."

    else

    echo "\n\nStudent failed..."

    fi

    fi14. Write a script to make following file and directory management operations

    menu based:

    Display current directory

    List directory

    Make directory

    Change directory

    Copy a file

    Rename a file

  • 8/8/2019 Os Final Ritesh

    16/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    16 Raish Vhora

    Delete a file

    Edit a file

    clear

    echo "\n 1 >> Display current directory :"echo "\n 2 >> List directory :"

    echo "\n 3 >> Make directory :"

    echo "\n 4 >> Change directory :"

    echo "\n 5 >> Copy a file :"

    echo "\n 6 >> Rename a file :"

    echo "\n 7 >> Delete a file :"

    echo "\n 8 >> Edit a file :"

    echo "\n Enter Your Choice :\c"

    read ch

    case $ch in

    1)

    dir;;

    2)

    ls;;

    3)echo "\n Enter Your Directory Name :\c"

    read dname

    mkdir $dname;;

    4)

    cd;;

    5)

    echo "\n Enter The Source File Name :\c"read name

    echo "\n Enter The Destination File Name :\c"

    read dname

    mv -i $name $dname;;

  • 8/8/2019 Os Final Ritesh

    17/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    17 Raish Vhora

    6)

    echo "\n Enter The Old File Name :\c"

    read name

    echo "\n Enter The New File Name :\c"

    read name

    mv -i $name $dname;;

    7)

    echo "\n Enter The File For Delete :\c"

    read name

    rmdir $name;;

    *)

    echo"Invalid Choice :"

    esac15. Write a script which reads a text file and output the following

    Count of character, words and lines.

    File in reverse.

    Frequency of particular word in the file. Lower case letter in place of upper case letter.

    clear

    echo "\nEnter The File Name :\c"

    read fname

    if [ -f $fname ]

    then

    echo "\n= = = = = = = = = = = = = = = = = = = = \n"

    echo "\n\t Menu Driven \n"

    echo "\n\t= = = = = = = = = = = = = = = = = = = = \n"

    echo "\n\t 1 >> Count of character, words and lines.\n"

  • 8/8/2019 Os Final Ritesh

    18/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    18 Raish Vhora

    echo "\n\t 2 >> File in reverse.\n"

    echo "\n\t 3 >> Frequency of particular word in the file.\n"

    echo "\n\t 4 >> Lower case letter in place of upper case letter.\n"

    echo "\n\t 5 >> Exit\n"

    echo "\n\t= = = = = = = = = = = = = = = = = = = = \n"

    echo "\n Enter Your Choice . . . \c"

    read c

    case $c in

    1)

    c=`cat $fname | wc -c`

    l=`cat $fname | wc -l`

    w=`cat $fname | wc -w`

    echo "\n The Number of Character In FIle Is : "$c

    echo "\n The Number Of Line In File Is : "$l

    echo "\n The Number Of Word In File Is :"$w ;;

    2)

    rev $fname;;

    3)

    echo "\n Enter The Word :\c"

    read word

    t=`grep -wc $word $fname`

    echo "\n The Number Of Word In File Is Available "$t "Times";;

    4)

    echo " Lower Into UpperCase :"

    t=`cat $fname | tr "a-z" "A-Z"`

  • 8/8/2019 Os Final Ritesh

    19/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    19 Raish Vhora

    echo $t

    echo " \n UpperCase Into LowerCase:"

    q=`cat $fname | tr "A-Z" "a-z" `

    echo $q;;

    5)exit;;

    esac

    else

    echo "\n File Does Not Exit "

    fi16. Write a shell script to check whether the named user is currently logged in or

    not.

    echo "enter the user name:- \c"

    read unm

    c=`who -u | grep -wc $unm`

    if [ $c -eq 0 ]

    then

    echo "$unm is not log in"

    elseecho "$unm is log in"

    fi

    17. Write a Script for Simple Database Management System Operation.

    Database File Contains Following Fields.

    EMP_NO

    EMP_NAME

    EMP_ADDRESS

    EMP_AGE

    EMP_GENDER

    EMP_DESIGNATIONEMP_BASIC_SALARY

    Provide Menu Driven Facility For

    VIEW RECORD BASED ON QUERY

    ADD RECORD

    DELETE RECORD

    MODIFY RECORD.

  • 8/8/2019 Os Final Ritesh

    20/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    20 Raish Vhora

    COUNT TOTAL NUMBER OF RECORDS

    EXIT

    fnm=emp.lst

    while echo "Operating On Employee Data " ;do

    echo " ####### Menu #######"

    echo "1. View File Data"

    echo "2. View Record"

    echo "3. Add REcord"

    echo "4. Delete Record"

    echo "5. Modify Record"

    echo "6. Exit"

    echo " ###################"

    echo "Select your Choice :- \c "

    read ch

    case "$ch" in

    1)

    cat $fnm

    ;;

    2)

    while echo "Enter The Emp No :- \c " ;do

    read eno

    set `cut -d "|" -f1 $fnm |grep -c "$eno"`

    if [ $1 -ne 0 ] ; then

  • 8/8/2019 Os Final Ritesh

    21/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    21 Raish Vhora

    cut -d "|" -f1-7 $fnm | grep "$eno"

    break

    else

    echo "Record Not Found"

    continue

    fi

    done

    ;;

    3)

    while echo "Enter Emp No :- \c " ;do

    read eno

    if [ `cut -d "|" -f1 $fnm |grep -c "$eno"` -eq 1 ] ;then

    echo "Emp No is already Exist"

    continue

    else

    break

    fi

    done

    echo "Enter Emp Name :- \c "

    read ename

    echo "Enter Addr :- \c "

  • 8/8/2019 Os Final Ritesh

    22/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    22 Raish Vhora

    read addr

    echo "Enter age :- \c "

    read age

    echo "Enter Gender :- \c "

    read gend

    echo "Enter Designation :- \c "

    read desg

    echo "Enter Salary :- \c "

    read sal

    echo "$eno|$ename|$addr|$age|$gend|$desg|$sal" >> $fnm

    ;;

    4)

    while echo "Enter the Emp No :- \c" ;do

    read eno

    set `cut -d "|" -f1 $fnm| grep -c "$eno"`

    if [ $1 -ne 0 ] ; then

    echo "Are you sure do u want to delete :- \c"

    read ans

    case "$ans" in

    [yY]*)

    cut -d "|" -f1-7 $fnm| grep -v "$eno" > te.lst

  • 8/8/2019 Os Final Ritesh

    23/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    23 Raish Vhora

    #sed -n "$eno"'!p' $fnm > te.lst

    cp te.lst $fnm

    esac

    break

    else

    echo "Record Not Found"

    continue

    fi

    done

    ;;

    5)

    echo "\n Enter Emp No. :- \c"

    read eno

    echo "What u want to change :- "

    echo "1.name \n2.Addr \n3.age \n4.desg \n5.salary"

    echo "Enter Your Choice \c"

    echo "Enter Your Choice \c"

    read cho

    echo "`grep -n "$eno" $fnm`" > line

    lno=`cut -d ":" -f1 line`

    case "$cho" in

  • 8/8/2019 Os Final Ritesh

    24/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    24 Raish Vhora

    1) echo "Enter Name :- \c"

    fild=2;;

    2) echo "Enter Address :- \c "

    fild=3;;

    3) echo "Enter Age : - \c"

    fild=4;;

    4) echo "Enter Desgination :- \c"

    fild=6;;

    5) echo "Enter Salary :- \c"

    fild=7;;

    6)

    break;;

    *) echo "Invalid choice"

    fild=0;;

    esac

    if [ $fild -ne 0 ] ;then

    read new

    old=`cut -d "|" -f$fild line`

    sed "$ln"'s/'"$old"'/'"$new"'/' $fnm > te.lst

    cp te.lst $fnm

    fi

  • 8/8/2019 Os Final Ritesh

    25/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    25 Raish Vhora

    ;;

    esac

    done

    18. Write A Script To Perform Following String Operations Using Menu:

    COMPARE TWO STRINGS.

    JOIN TWO STRINGS.

    FIND THE LENGTH OF A GIVEN STRING.

    OCCURRENCE OF CHARACTER AND WORDS

    E. REVERSE THE STRING.

    clear

    echo "\n\t = = = = = = = = = = = = = = = = = ="

    echo "\n\t\t String Manipulation "

    echo "\n\t = = = = = = = = = = = = = = = = = ="

    echo "\n\t\t 1 >> COMPARE TWO STRINGS."

    echo "\n\t\t 2 >> JOIN TWO STRINGS."

    echo "\n\t\t 3 >> FIND THE LENGTH OF A GIVEN STRING."

    echo "\n\t\t 4 >> OCCURRENCE OF CHARACTER AND WORDS"

    echo "\n\t\t 5 >> REVERSE THE STRING."

    echo "\n\t\t 6 >> Exit"

    echo "\n\t = = = = = = = = = = = = = = = = = ="

    echo "\n\t\t Enter Your Choice :\c"

    read ch

    case $ch in

    1)

    echo "\n Enter The First String :\c "

    read str

    echo "\n Enter The Second String :\c "

    read str1

    if [ $str = $str1 ]

    then

    echo "Both Strings Are Same "

    else

  • 8/8/2019 Os Final Ritesh

    26/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    26 Raish Vhora

    echo "Both Strings Are Different"

    fi

    ;;

    2)

    echo "\n Enter The First String :\c "

    read s1

    echo "\n Enter The Second String :\c "

    read s2

    str3=$s1$s2

    echo $str3

    ;;

    3)

    echo "\n Enter The String :\c "

    read str

    l=${#str}

    echo "\n The Length Of String Is "$l

    ;;

    4)

    echo "\n Enter The String :\c "

    read str

    c=`echo $str | wc -c`

    echo "\n The Number Of The Character Is :"$c

    w=`echo $str | wc -w`

    echo "The Occurence Of the Word Is :"$w

    ;;

    5)

    echo "\n Enter The String :\c "

    read s

    l=${#s}

    rev=""

  • 8/8/2019 Os Final Ritesh

    27/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    27 Raish Vhora

    while [ $l -ge 1 ]

    do

    ch=`echo $s | cut -c $l`

    rev=$rev$ch

    l=`expr $l - 1`

    done

    echo "reverse string is : "$rev

    ;;

    6)exit;;

    esac19. Write a script to calculate gross salary for any number of employees

    Gross Salary =Basic + HRA + DA.

    HRA=10% and DA= 15%.

    clear

    echo "\n How Many Numbers You Want :\c"

    read no

    while [ $no -gt 0 ]

    do

    echo "\n Enter Basic Salary :\c"

    read bs

    hra=`expr $bs \* 10`

    hra=`expr $hra / 100`

    da=`expr $bs \* 15`

    da=`expr $da / 100`

    gs=`expr $bs + $hra + $da`

    echo "\n Gross Salary Is "$gs

    no=`expr $no - 1`

    done

  • 8/8/2019 Os Final Ritesh

    28/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    28 Raish Vhora

    20. Write a script to check whether a given string is palindrome or not.

    clear

    echo "enter string:- \c"

    read s

    l=${#s}

    echo "length of string is:- "$l

    rev=""

    while [ $l -ge 1 ]

    do

    ch=`echo $s | cut -c$l`

    rev=$rev$ch

    l=`expr $l - 1`

    done

    echo "reverse is" $rev

    if [ $rev = $s ]

    then

    echo "palindrom"

    else

    echo "not palindrom"

    fi

    21. Write a script to check whether a given number is palindrome or not.

    clear

    echo "\n Enter The Number :\c"read no

    t=$no

    rev=0

    while [ $no -gt 0 ]

    do

    r=`expr $no % 10`

    rev=`expr $rev \* 10`

    rev=`expr $rev + $r`

    no=`expr $no / 10`

    done

    echo "\n The Original Number Is :"$t

    echo "\n The Revarse Number Is :"$rev

    if [ $rev -eq $t ]

    then

    echo palindrom

  • 8/8/2019 Os Final Ritesh

    29/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    29 Raish Vhora

    else

    echo not palindrom

    fi22. Write a script to display all words of a file in ascending order.

    echo "Enter File Name \c"read fname

    if [ -f $fname ]

    then

    echo "==================================================="

    echo "Original File Content is "

    echo "==================================================="

    cat $fname

    echo "==================================================="

    echo "File words in ascending order is"echo "==================================================="

    set cat $fname`

    for i in $*

    do

    echo "$i" >> temp

    done

    sort temp

    rm temp

    echo "==================================================="

    elseecho "File does not exist"

    fi23. Write a script to display all lines of a file in ascending order.

    echo "Enter File Name \c"

    read fname

    if [ -f $fname ]

    then

    echo "==================================================="

    echo "Original File Content is "echo "==================================================="

    cat $fname

    echo "==================================================="

    echo "File lines in ascending order is"

    echo "==================================================="

    cat $fname | sort

    else

  • 8/8/2019 Os Final Ritesh

    30/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    30 Raish Vhora

    echo "File does not exist"

    fi24. Write a script to display the last modified file.

    clear

    fname=`ls -lt | tr -s ' ' | head -2 | tail -1 | cut -d ' ' -f8`ctime=`ls -lt | tr -s ' ' | head -2 | tail -1 | cut -d ' ' -f6,7`

    echo Last modified File is $fname

    echo Last File Modification time is $ctime

    25. Write a shell script to add the statement #include at the beginning of

    every C source file in current directory containing printf and fprintf.

    while true; do

    read -e -p "Enter Directory: " path || exit

    [[ -d $path ]] && breakecho "Invalid path! Try Again!"

    done

    path=${path%/}

    myargs=`grep -l -e "printf" -e "fprintf" $path/*.c | xargs`

    if [ $? -gt 1 ]; then # grep exits with status 1 when no matches were found.

    echo -n "No Matches were found. " && exit

    fi

    temp=$(mktemp tmp.XXXXXXXXX)

    for i in $myargs # Here, grep has the exit status 0.do

    echo "Do you want to add '#include ' to $i?"

    read S

    case $S in

    Y|y|YES|Yes|yes|yeah)

    sed '1i\

    #include' "$i" > "$temp" # i for insertion, 1 for 1st line. $i is the file to

    insert. and all output will be redirected to $temp

    mv "$temp" "$i" # renaming $temp by over writing to $i

    ;;

    n|N|NO|no|No|nope)

    echo "Alright! Next.."

    shift

    ;;

  • 8/8/2019 Os Final Ritesh

    31/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    31 Raish Vhora

    *)

    echo "Invalid input."

    ;;

    esac

    done

    if [ -z $myargs ]; then

    echo "No Matches were found. Try another Directory"

    else

    clear

    head -n5 $path/*.c | less

    fi

    rm $temp

    26. Write a script that behaves both in interactive and non-interactive mode.

    When no arguments are supplied, it picks up each C program from current

    directory and lists the first10 lines. It then prompts for deletion of the file. If the

    user supplies arguments with the script, then it works on those files only.

    clear

    if [ $# -eq 0 ]

    then

    set `ls *.c`for i in $*

    do

    if [ -f $i ]

    then

    echo "file name:- $i"

    head 10 $i | cat

    echo "do you want delete file $i [y/n]:- \c"

    read ans

    if [ $ans = 'Y' -o $ans = 'y' ]

    then rm $i

    fi

    echo "enter any key to continue"

    read ans

    fi

    done

  • 8/8/2019 Os Final Ritesh

    32/32

    K.P.Patel School of Management & Computer Studies

    (KSMCS)MCA

    else

    for i in $*

    do

    if [ -f $i ]

    then

    echo "file name:- $i"

    head 10 $i | cat

    echo "do you want delete file $i [y/n]:- \c"

    read ans

    if [ $ans = 'Y' -o $ans = 'y' ]

    then

    rm $i

    fi

    echo "enter any key to continue"

    read ans

    fi

    done

    fi27. Write a script that deletes all leading and trailing spaces in all lines in a file.

    Also remove blank lines from a file. Locate lines containing only printf but not

    fprintf.

    echo Enter the file name

    read a'

    b=$(echo ${$a} | sed -e 's/^ *//g;s/ *$//g')

    echo "a->[${$a}] b->[${b}]"

    OR

    sed 's/^[ \t]*//;s/[ \t]*$//' yourfilename