1 / 36

UNIX PROGRAMS & LOOPS

UNIX PROGRAMS & LOOPS. #QUE:1 WRITE A UNIX SHELL SCRIPT TO #DISPLAY THE TYPE OF SHELL THAT ARE #CURRENTLY# LOGGED IN USER IS USING.IT #SHOULD DISPLAY THE MESSAGE AS BELOW. # <TYBCA - SEM 5> echo $"user name:" $LOGNAME echo $"Shell:" $SHELL.

nuri
Download Presentation

UNIX PROGRAMS & LOOPS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIX PROGRAMS & LOOPS

  2. #QUE:1 WRITE A UNIX SHELL SCRIPT TO #DISPLAY THE TYPE OF SHELL THAT ARE #CURRENTLY# LOGGED IN USER IS USING.IT #SHOULD DISPLAY THE MESSAGE AS BELOW. # <TYBCA - SEM 5> echo $"user name:" $LOGNAME echo $"Shell:" $SHELL

  3. #QUE:2 WRITE A UNIX SHELL SCRIPT TO ACCEPT THE EMPLOYEE NAME FROM THE USER AND DISPLAY APPROPRIATE MESSAGE.ASSUME EMPLOYEE IS WORKING #ON A SINGLE PROJECT DETAILS ARE STORED IN FILE THE EMPLOYEE ARE #STORE IN FILE STATUS.DAT.# <TYBCA - SEM 5> echo -e "Enter Name of the employee:\c" read name ecd=0 rm=0 if ecd=`grep "$name" project.dat | cut -d "|" -f 1` then dw=`grep "$ecd" struct.dat | cut -d "|" -f 3` du=`grep "$ecd" project.dat | cut -d "|" -f 4` else echo -e "===Name Not Found===\n" fi if [ $dw -eq $du ] ; then echo "$name,Today is the last day to finished your project" else rm=`expr $du - $dw` if [ $rm -le 30 ] ; then echo "$name,Hurry! only $rm days are remaining to finish your project" else echo "$name,schedule yourself you have still $rm days to finish the job" fi fi

  4. #QUE:3 WRITE A UNIX SHELL SCRIPT WHICH TAKES I/P A FILENAME AND CHECKS IT TO SEE # IF IT IS A ORDINARY FILE AND READABLE, IT CONTAINS SHOULD BE DISPLAYED. # <TYBCA - SEM 5># echo -e "Enter the file name: \c" read fname if [ -f $fname ] then if [ -$fname ] then cat $fname fi fi

  5. #QUE:4 WRITE A UNIX SHELL SCRIPT TO FIND THE SUM OF FIRST N NUMBERS WHERE N IS # ACCEPTED FROM USER. # e.g 1+2+3...+n # <TYBCA - SEM 5> # echo -e "Enter the value of n: \c" read n sum=0 i=1 while [ $i -le $n ] do sum=`expr $sum + $i` i=`expr $i + 1` done echo The sum is: $sum

  6. QUE:5 WRITE A UNIX SHELL SCRIPT TO ACCEPT MARKS OF MATH,SCI,ENG AND CALCULATE # THE AVG OF MARKS AND DISPLAY THE MESSAGE. # <TYBCA - SEM 5> echo "Enter the marks of maths:" read m echo "Enter the marks of science:" read s echo "Enter the marks of english:" read e (( avg=($m+$s+$e)/3 )) if [ $avg -gt 60 ]; then echo "FIRST CLASS" elif [ $avg -gt 50 ] && [ $avg -lt 60 ] then echo "SECOND CLASS" elif [ $avg -gt 40 ] && [ $avg -lt 50 ] then echo "THIRD CLASS" else echo "FAIL" fi

  7. #QUE:7 WRITE A UNIX SHELL SCRIPT WHICH PRINTS THE FOLLOWING: # A)THE MESSAGE "TODAY IS:" WITH CURRENT DATE IN MM/DD/YY FORMAT. # B)THE MESSAGE "NO OF USERS LOGGED IN:" WITH TOTAL NO OF CURRENT # LOGGED IN USERS. # C)THE MESSAGE "PERSONAL STATUS:" WITH YOU OWN LOGNAME,TERMINAL NUMBER, # DATA AND TIME OF LOGIN. # echo -e "Todays Date: \c" date +"%D" echo " " echo -e "NO. of user currently logged on: \c" who | wc -l echo " " echo -e "Personal information" who am i -H echo " "

  8. #QUE:8 WRITE A UNIX SHELL SCRIPT THAT EXAMINES ALL THE NUMBERS FROM 1 TO 199 # DISPLAYING ALL THOSE WHICH THE SUM OF THE CUBES OF THE DIGITS EQUALS # THE NUMBER ITSELT. # <TYBCA - SEM 5> echo The List of the digits. i=1 while [ $i -le 999 ] do no=$i sum=0 while [ $no -gt 0 ] do (( j = no%10 )) (( sum = sum + (j * j * j))) ((no = no / 10)) done if [ $sum -eq $i ] then echo " " echo $i fi ((i = i + 1)) done

  9. #QUE:9 WRITE A UNIX SHELL SCRIPT TO ACCEPT THE SALESMAN CODE FROM USER AND THEN # DISPLAY ALL THE ORDER NUMBERS AND ORDER AMT FOR THAT SALESMAN CODE. ALSO # DISPLAY THE TOTAL ORDER AMT # <TYBCA - SEM 5> echo -e "Enter salesman code: \c" read scode sum=0 echo Order no Order amount grep "$scode" sales.dat | cut -f2,3 for i in `grep "$scode" sales.dat | cut -f3` do (( sum = sum + i)) done echo -e "\nTotal amount: \c" echo $sum

  10. #QUE:11 WRITE A UNIX SHELL TO ADD RECORDS TO A FILE CALLED MATERIAL.DAT #THE FIELD USING MATCODE,QTY,SOLD AND RATE. # <TYBCA - SEM 5> mcode=`tail -1 mat.dat | cut -d"|" -f1` echo -e "\nEnter the quntity of sold item: \c" read qty echo -e "\nEnter the amount: \c" read amt if [ $qty -lt 1 ] ; then echo -e "\nQuantity should be greater then 0" fi if [ $amt -le 100 -o $amt -gt 10000 ] then echo -e "\nAmount must between 100 and 10000" fi (( mcode = mcode + 1 )) echo "$mcode | $qty | $amt" >> mat.dat

  11. #QUE:12 WRITE A UNIX SHELL SCRIPT WHICH ACCEPTS A CHOICE FOR THE USER AND # EXECUTE THE CORRESPONDING PROGRAMS USING CASE STRUCTURE. ASSUMING # THE PROGRAM AS (ADD,MODIGY & DELETE) ALREADY EXITS THE FORMAT OF THE # MENU IS # A -> ADDITION # M -> MODIFICATION FO RECORDS # D -> DELETION OF RECORDS # Q -> QUIT # <TYBCA - SEM 5> echo 1. Addition of record echo 2. Modify a record echo 3. Deletion of record echo 4. Quit echo -e "Enter your choice: \c" read ch cat mat.dat case "$ch" in A) mcode=`tail -1 mat.dat | cut -d"|" -f1` echo -e "\nEnter the quntity of sold item: \c" read qty echo -e "\nEnter the amount: \c" read amt (( code = mcode + 1 )) echo "$code | $qty | $amt" >> mat.dat;;

  12. M) echo -e "\nWhich record u want to change?: \c" read r echo -e "\nEnter new quantity: \c" read nqty echo -e "\nEnter new rate: \c" read nrate sed ''$r' c\ $r' | '$nqty' | '$nrate'' mat.dat >ext cat ext > mat.dat;; D) echo -e "Enter record no: \c" read rn echo -e "Are you sure to delete this record?(Y or N): \c" read cho if [ $cho = 'Y' ] then sed ''$rn' d' mat.dat >ext fi;; Q)esac

  13. echo -e "ENTER BOOK NAME:" read bname grep "$bname" book.dat

  14. #QUE:14 WRITE A SHELL PROGRAM EHICH WILL WORK LIKE COMMAND WC. # <TYBCA - SEM 5> # rd=0 terminal=/dev/pts/0 exec<$1 while read line do (( ln = ln + 1 )) set $line (( word = word + $# )) done echo "Total no. of lines: $ln" echo "Total no of words: $word" exec<$terminal

  15. #QUE:16 WRITE A SHELL SCRIPTS TO COUNT NUMBER OF VOWELS IN FILE #IIGNORING THE CASE. # <TYBCA - SEM 5> # echo -e "Enter a file name: \c" read fname tot=`wc -c < $fname` count=0 while [ $tot -gt 0 ] do for ch in `cut -c $tot < $fname` do case $ch in a|A)(( count = count + 1 ));; e|E)(( count = count + 1 ));; i|I)(( count = count + 1 ));; o|O)(( count = count + 1 ));; u|U)(( count = count + 1 ));; esac done (( tot = tot - 1 )) done

  16. QUE:17 WRITE A SHELL SCRIPTS TO GENERATE RANDOM NUMBER.ALLOW USER TO GUESS 6 # TIMES TO GET THE NO.PRINT APPROPRIATE MESSAGES AGTER EACH GUESS # RANDOM GENERATED IS 100 USERS ENTERS 98 MESG: TO SMALL # USER ENTERS 104 THE MESG TO BIG# <TYBCA - SEM 5> ctr=0 while true do s=`date "+%S"` m=`date "+%M"` let rn=s\*m let ctr=ctr+1 echo -e "Enter your guess \c" read gu echo "Random Generated Is: $rn" if [ $gu -gt $rn ] ; then echo "Too Big" elif [ $gu -lt $rn ] ; then echo "Too Small" else echo "Equals" fi if [ $ctr -eq 5 ] ; then exit fi done

  17. # To Remove Directory and its subdirectory when user enters the name of # directory echo "Enter name of directory" read dname echo "This Will remove $dname and all the subdirectories below it" echo -e "Enter y/n :\c" read ch if [ $ch == "n" -o $ch == "N" -o -z $dname ] ; then exit 1 fi cd $dname while true ; do for i in `ls` ; do if [ -d $i ] ; then dir="true" dn=$i break; else dir="false" fi done

  18. if [ $dir == "true" ] ; then cd $dn else for i in `ls` ; do rm $i done ds=`pwd` dn=${ds##*/} cd .. rmdir $dn fi if [ $dn == $dname ] ; then break fi done

  19. QUE:19 WRITE A UNIX SELL SCRIPT THAT TAKES A LOGIN AS A COMMAND LING #ARGUMENT AND REPORTS TO YOU WHEN THAT PERSON LOGS IN.HAVE IS SEND #A GREETING TO THAT PERSONS EXPLAIN WHO WOULD YOU RUN SUCH A SHELL #SCRIPT. <TYBCA - SEM 5> if [ $ -lt 1 ] then echo -e "\nPlz. enter the username as command line argument" exit fi lname=$1 while true do who | grep "$lname" > /dev/null if [ $?=0 ] then echo -e "\n$lname is logged in" echo -e "\n$lname logged in time: \c" date "+%I":"%M" echo "Welcome to the world of unix" | write $lname exit else sleep 60 fi done

  20. #QUE:20 WRITE A SHELL SCRIPT THAT TAKES A COMMAND LINE I/P N AND A WORD. IT # THEN PRINTS THE WORD N TIMES,ONE WORD PER LINE. # <TYBCA - SEM 5> i=0 while [ $i -lt $1 ] do echo $2 (( $i = $i + 1 )) done

  21. #QUE:21 WRITE A PROGRAM TO DISPLAY DATE IN DIFFERENT FORMAT ALONG WITH TIME. # <TYBCA - SEM 5> # <ROLLNO - 27> echo Today date and time in different format as foloows... echo date "+%A, %B %d, %Y, %I:%M:%S" echo date "+%d/%m/%y, %H:%M:%S" echo date "+%a, %b, %d, %H:%M:%S, %p" echo date "+%D, %T" echo

  22. #QUE:22 WRITE A UNIX SHELL SCRIPT TO FIND SUM OF DIGIT AND CHECK THE NUMBER IS PALINDROM OR NOT. <TYBCA - SEM 5> echo -e "Enter positive no: \c" read no if [ "$no" -le "0" ] then echo Plz. Enter a positive no exit fi sum=0 count=0 base=1 (( no1 = no )) while [ "$no" -gt "0" ] do (( sum = sum + (no % 10) )) (( no = no / 10 )) (( count = count + 1 )) (( base = base * 10 )) done lin=0

  23. k=0 (( no2 = no1 )) (( i = i + 1 )) echo -e "\nThe sum of digit is: $sum" if [ test $no2 -eq $palin ] then echo -e "\nThe number is Palindrome" else echo -e "\nThe number is not Palindrome" fi

  24. #QUE:41 WRITE A SHELL SCRIPT TO ACCEPT THE NAME OF THE USER AS #INPUT AND CHECK # OUT WHETHER THAT USER HAS LOG IN OR NOT. # <TYBCA SEM 5> echo " enter the login name...:" read ln finger | cut -c 1-11 >q41.txt if grep $ln q41.txt then echo "USER IS LOG IN" else echo "USER IS NOT LOG IN" fi

  25. ##QUE26 TAKE 4 NUMERIC VALUES FROM THE USER & FIND OUT MAX VALUE. WRITE A SHELL # SCRIPT FOR IT. # <TYBCA - SEM 5> echo -e "\nEnter 4 numbers" echo -e "\nEnter first no: \c" read first echo -e "\nEnter second no: \c" read second echo -e "\nEnter third no: \c" read third echo -e "\nEnter fourth no: \c" read fourth fs=0 tf=0 if [ $first -ge $second ] then (( fs = first )) else (( fs = second )) fi if [ $third -ge $fourth ]

  26. if [ $third -ge $fourth ] then (( th = third )) else (( th = fourth )) fi if [ $fs -ge $th ] then echo -e "\n$fs is the greatest no" else echo -e "\n$th is the greatest no" fi

  27. #QUE:27 WRITE A SHELL SCRIPT TO DISPLAY THE FOLLOWING INFORMATION. # YOUR LOGIN DIRECTORY IS <LOGIN DIR NAME> # YOUR PRIMARY PROMPT IS <PRIMARY PROMPT> # NOW CURRENT MONTH IS <MONTH> # FILES CAN BE EXECUTED AT <PATH> # <TYBCA - SEM 5> # < echo "YOUR LOGIN DIRECTORY IS:" $HOME echo "YOUR PRIMARY PROMPT IS:" echo "NOW CURRENT MONTH IS:" `date +%B` echo "FILES CAN BE EXECUTED AT:" $PATH

  28. #QUE:29 WRITE A SHELL SCRIPT FOR ACCEPTING THE FOLLOWING INFORMATION AND STORING # IT IN A FILE. # CASSATTE NO. MOVIE NAME. LANGUAGE. PRICE AND DATE OF RELEASE. # <TYBCA - SEM 5> # <SEM - 27> ans="y" while [ $ans == "y" ] do echo "Enter the cassatte no:" read cn echo "Enter the Movie name:" read mn echo "Enter the language:" read ln echo "Enter the price:" read p echo "Enter the date or release:" read dr echo " $cn | $mn | $ln | $p | $dr ">>movie.dat echo "Do you want to continue?[Y/N]" read ans done

  29. # #QUE:31shell procedure to delete the specified record from the # library file. It should accept a book number, check whether it is # existing. If so, display the current details, ask for confirmation # and then delete it from the file. # <TYBCA SEM 5> clear echo -n "ENTER NUMBER OF THE BOOK:=" read Bn c=`grep -c "$Bn" lib1.txt` if [ $c -gt 0 ] then grep "^$Bn" lib1.txt > list2 awk -F "|" '{ printf $1,$2,$3,$4,$5 }' list2 echo `grep $Bn lib1.txt` echo "DO YOU WANT TO DELETE(Y OR N)?" read ans

  30. if [ $ans = 'y' -o $ans = 'Y' ] then echo "THE BOOK IS DELETED" grep -v "^$Bn" lib1.txt > list1 mv list1 lib1.txt else echo "BOOK IS NOT DELETED" fi else echo "FILE DOES NOT EXIST" fi

  31. # QUE:32 Write a shell scripts which finds the occurence of a given word from file echo -n "ENTER THE FILENAME:=" read fn1 awk ' BEGIN { occ = 0 printf "\n ENTER THE WORD TO BE SEARCHED:=" getline wd < "/dev/tty" printf "\n WORD IS:= %s",wd } { i = 0 split($0,ar," ") while( i <= NF ) { i = i+1 if (match(ar[i],wd)) occ++ } } END { printf "\n NO. OF OCCURENCE %d \n ",occ }‘ $fn1

  32. # #QUE:33 WRITE A SHELL SCRIPT TO FIND THE SQUARE ROOT OR A EN NUMBER. # <TYBCA SEM 5> echo "ENTER THE NO:" read no echo "The number is $no" echo "sqrt($no)" | bc -l

  33. ##QUE:38 Write a shell scripts to accept a string in the command line and # reverse the same. # <TYBCA SEM 5> echo $# echo $* > q1 len=$(cat q1 |wc -c) i=`expr $len - 1` echo $i m=" " while [ $i -gt 0 ] do m1=$(cut -c $i q1) echo $m1 m=`echo $m$m1` let i=i-1 done echo $m rm q1

  34. #QUE:39 Write a shell script to accept the number below 50 and display the # square of each this should continue as long as the user wishes. ch='y' while [ $ch = 'y' -o $ch = 'Y' ] do s=0 n=0 flag=0 while [ $flag -eq 0 ] do echo -n "ENTER THE NO:=" read n if [ $n -lt 50 ] then flag=1 else echo "NUMBER MUST BE LESS THEN 50" fi done s=0 s=`expr $n \* $n` echo "THE SQUARE OF $n IS:= $s" echo "DO YOU WANT TO CONTINUE PRESS:= Y/N" read ch done

  35. #QUE:40 Write a shell scripts to accept a name of user as input and check if # the same is present in /etc/passwd file. # <TYBCA SEM 5> clear echo "ENTER THE NAME OF USE TO CHECK IF IT PRESENT IN /etc/passwd" read name if [ -z $name ] then echo "YOU HAVE ENTER THE NULL STRING" else grep "$name" /etc/passwd && printf "FOUND" || echo "USER IS NOT FOUND" fi #QUE:41 WRITE A SHELL SCRIPT TO ACCEPT THE NAME OF THE USER AS INPUT AND CHECK OUT WHETHER THAT USER HAS LOG IN OR NOT. # <TYBCA SEM 5> echo " enter the login name...:" read ln finger | cut -c 1-11 >q41.txt if grep $ln q41.txt then echo "USER IS LOG IN" else echo "USER IS NOT LOG IN" fi

More Related