Sem 3 Unix
Sem 3 Unix
/bin/sh clear echo "Enter the first file name" read a echo "Enter the second file name" read b echo "Contents in" $a cat $a echo "Contents in" $b cat $b cat $a $b > file3 cat file3 sleep 5 OUTPUT ------------Enter the first file name /home/mcai/y65532/shell/f1 Enter the second file name /home/mcai/y65532/shell/f2 Contents in /home/mcai/y65532/shell/f1 this is my first shell program Contents in /home/mcai/y65532/shell/f2 this is my second shell program contents in file3 this is my first shell program this is my second shell program
1.(b) Write a Shell command to list all the files in the /tmp directory Owned by the user root. SCRIPT -----[y65532@aditya unix1]$ ls -l /tmp | grep "root" drwx------ 2 root root 16384 Dec 19 2003 lost+found drwx------ 2 root root 8192 Oct 20 19:47 orbit-root -rw------- 1 root root 0 Oct 30 08:19 session_mm.sem
1.(c) Construct a pipeline to count number of users currently logged in. SCRIPT -----[y65532@aditya unix1]$ echo The number of users logged are: `who|wc -l` The number of users logged are: 23
2.(a) Use grep command to find each line of a given file that contains some required pattern. SCRIPT ---------#! /bin/sh echo Enter the word: read word echo Enter the filename: read file echo The pattern lines are: grep $word $file OUTPUT ------------[y65532@aditya unix1]$ sh 2a Enter the word: echo Enter the filename: 1 The pattern lines are: echo "Enter the first file name" echo "Enter the second file name" echo "Contents in" $a echo "Contents in" $b 2.(b) Compare two files, displaying all differences in another file. SCRIPT -----------#! /bin/sh echo Enter first filename: read a echo Enter second filename: read b echo Contents in $a file: cat $a sleep 1 echo Contents in $b file: cat $b echo diff $a $b >temp2 echo The difference is: cat temp2
3
OUTPUT -----------[y65532@aditya unix1]$ sh 2b Enter first filename: 1 Enter second filename: 2a Contents in 1 file: #! /bin/sh clear echo "Enter the first file name" read a echo "Enter the second file name" read b echo "Contents in" $a cat $a echo "Contents in" $b cat $b cat $a $b > file3 cat file3 sleep 5 Contents in 2a file: #! /bin/sh clear echo Enter the word: read word echo Enter the filename: read file echo The pattern lines are: grep $word $file The difference is: 2d1 < 4,14c3,8 < echo "Enter the first file name" < read a < echo "Enter the second file name" < read b < echo "Contents in" $a < cat $a < echo "Contents in" $b < cat $b
4
< cat $a $b > file3 < cat file3 < sleep 5 --> echo Enter the word: > read word > echo Enter the filename: > read file > echo The pattern lines are: > grep $word $file
3. Write a Shell command to sort the / etc/passwd file, place the Results in the file called foo. SCRIPT ---------[y65532@aditya unix1]$ sort /etc/passwd >foo
4. Write a Shell program to reverse a given five-digit number. SCRIPT ---------#!/bin/sh echo Enter any number read n rev=0 for((i=0;n!=0;i++)) do { r=`expr $n % 10` rev=`expr $rev \* 10 + $r` n=`expr $n / 10` } done echo The reverse of given number is $rev sleep 2 OUTPUT ----------Enter any number: 12345 The reverse of the given number is: 54321
5. (a) Print the numbers from 1 to 10 . SCRIPT ---------#! /bin/sh echo Enter the value of n: read n echo The numbers are: for (( i=1; i<=n; i++)) do echo $i done OUTPUT ----------Enter the value of n: 6 The numbers are: 1 2 3 4
5. (b) Write a Shell script that reports how many bytes are used by Regular files in a directory. SCRIPT ---------#!/bin/bash echo Enter the directory name read a if [ -d $a ] then bytecount=0 for i in `ls $a` do if [ -f $i ] then bytes=`ls -l $a/$i|tr -s ' '|cut -d ' ' -f5` bytecount=`expr $bytecount + $bytes` fi done echo bytes in use by regular files is $bytecount else echo $a is not a directory fi sleep 2
8
OUTPUT ----------[y65532@aditya unix1]$ sh 5b Enter the directory name /home/mcai/y65532/unix1 bytes in use by regular files is 11388
6. Write a shell script to mail a letter to the list of users in a file called"mylist" . SCRIPT ---------#!/bin/sh echo "Enter a file name" read file for person in `cat mylist` do mail -s "Hi! How are you ?" $person <$file echo "mail sent to " $person done sleep 2 OUTPUT -----------Enter a file name mylist mail sent to y55555 mail sent to y55552 mail sent to y55558
10
7. Create a script file that backup file names starting with letter 'f' with Extension '. bak', creating a directory 'backup' . SCRIPT ---------#!/bin/sh echo "Enter a directory name" read dd mkdir $dd for i in `ls f*.bak` do cp $i $dd/$i done echo backup is done ls -l $dd sleep 2 OUTPUT -----------Enter a directory name temp1 backup is done total 4 -rw-r--r-- 1 y55555 mcai
11
8. (a) Set the executable permission on all plane files. SCRIPT ----------#!/bin/sh echo Enter the directory name read fname for n in * do chmod -x $n done OUTPUT -----------Enter the directory name /home/mcai/y65532/unix1 8.(b) Write a shell script called 'quiz' that confirms the shell that u are Using. SCRIPT ---------#!/bin/bash clear echo Enter the shell name read name if [ $name = $SHELL ] then echo This is the current shell else echo This is not the current shell fi OUTPUT -----------[y65532@aditya unix1]$ sh 8b Enter the shell name /bin/bash This is the current shell
12
9. (a) Write a shell script to check whether given user is logged or not. SCRIPT ---------echo To find out whether the given user is logged or not echo Enter the number read num if who | grep $num then echo logged in else echo logged out fi OUTPUT -----------To find out whether the given user is logged or not Enter the number y55555 y55555 pts/57 Oct 31 10:42 (123.0.55.23) logged in 9. (b) Write a shell script to display a message, when the given user Logged in. SCRIPT ---------#!/bin/bash clear echo Enter the logname read name who | grep $name >tmp if [ $? -eq 0 ] then echo hai, this is a message to $name else echo the given user is not looged fi OUTPUT -----------[y65532@aditya unix1]$ sh 9b Enter the logname y55555 hai, this is a message to y55555
13
10. Write a shell script that performs arithmetic operations on the Given numeric arguments. SCRIPT ----------#! bin/sh #To perform arithmetic operations echo Enter the values read value1 value2 echo Addition of two numbers is `expr ${value1} + ${value2}` echo echo Subtraction of two numbers is `expr ${value1} - ${value2}` echo echo Multiplication of two numbers is `expr ${value1} \* ${value2}` echo echo Division of two numbers is `expr ${value1} / ${value2}` echo OUTPUT ----------[y65532@aditya unix1]$ sh 10 Enter the values 64 Addition of two numbers is 10 Subtraction of two numbers is 2 Multiplication of two numbers is 24 Division of two numbers is 1
14
11. Obtain a list of users who have unread mail (mail size > 0) SCRIPT ---------#!/bin/sh echo '***************************************************' echo List of users who have unread mail echo '***************************************************' echo ls -l /var/spool/mail | tr -s ' ' | cut -f 5,9 -d " " | awk ' ( $1 > 0 ) { print $2 "has unread mail" } ' sleep 2 OUTPUT ----------[y65532@aditya unix1]$ sh 11 ****************************************** List of users who have unread mail ****************************************** glakshmihas unread mail kiranhas unread mail mca1has unread mail mmadhavihas unread mail roothas unread mail sivaramhas unread mail t02has unread mail t03has unread mail y55506has unread mail y55507has unread mail y55509has unread mail y55510has unread mail y55511has unread mail
15
12.(a) Write a shell script to greet any of your 4 friends with a for loop, Proceeded by the string "Hi". SCRIPT ---------#!/bin/bash echo Hai to everyone for i in y65532 y55552 y55555 do echo $i hi done OUTPUT ----------[y65532@aditya unix1]$ sh 12a Hai to everyone y65532 hi y55552 hi y55555 hi 12.(b)Write a shell script to greet any of your 4 friends with a for loop, Preceded by the string "Hi", accepting friends names from the command prompt. SCRIPT ---------echo Hai to everyone for i in $@ do echo $1 hi shift done OUTPUT -----------[y65532@aditya unix1]$ sh 12b y55555 y55558 y55546 Hai to everyone y55555 hi y55558 hi y55546 hi
16
13. Write a Shell program to sort given numbers in ascending order. SCRIPT ----------#!/bin/sh echo Enter the value of n read n echo Enter the elements into the array: for (( i=1; i<=$n; i++ )) do echo Enter the value of $i read a[$i] done for (( i=1;i<=$n-1; i++)) do for (( j=1;j<=$n-$i;j++ )) do k=`expr $j + 1` if [ ${a[$j]} -gt ${a[k]} ] then temp=${a[$j]} a[$j]=${a[k]} a[k]=$temp fi done done echo The sorted elements are: for (( i=1;i<=$n;i++ )) do printf "%3d" ${a[$i]} done echo sleep 2 OUTPUT ----------[y65532@aditya unix1]$ sh 13 Enter the value of n 4 Enter 4 elements into the array: 8 1 2 4
17
18
14. Write a Shell program to exchange the values of two variables. SCRIPT ---------#!/bin/bash echo Enter any two numbers read a b echo The given numbers before swapping are echo $a $b t=$a a=$b b=$t echo The given numbers after swapping are echo $a $b sleep 2 OUTPUT ----------[y65532@aditya unix1]$ sh 14 Enter any two numbers 58 The given numbers before swapping are 58 The given numbers after swapping are 85
19
15. Find all lines in a file with words longer than four characters, Assuming that words are separated by spaces except at the beginning or at the end. SCRIPT ---------#!/bin/sh echo Enter file name read fname n=`cat $fname |wc -l` i=1 while [ $i -le $n ] do line=`cat $fname |head -$i |tail +$i` words=`echo $line|tr -s " "|wc -w` w=1 while [ `expr $w - 1 ` -lt $words ] do str=`echo $line|tr -s " "|cut -d " " -f$w` count=`echo $str|wc -c` count=`expr $count - 1` if [ $count -gt 4 ] then echo $i $line break fi w=`expr $w + 1` done i=`expr $i + 1` done sleep 2 OUTPUT -----------Enter file name p15 1 echo Enter file name 2 read fname 3 n=`cat $fname |wc -l` 5 while [ $i -le $n ] 7 line=`cat $fname |head -$i |tail +$i` 8 words=`echo $line|tr -s " "|wc -w` 10 while [ `expr $w - 1` -lt $words ] 12 str=`echo $line|tr -s " "|cut -d " " -f$w` 13 count=`echo $str|wc -c` 14 count=`expr $count - 1`
20
21
16. Write a Shell script to sort the given numbers in descending order Using bubble sort algorithm. SCRIPT ---------#!/bin/bash echo Enter the array size read n echo Enter the array elements for (( i=0; i<$n; i++ )) do read a[$i] done echo The elements before sorting are for (( i=0; i<$n; i++ )) do echo ${a[$i]} done for (( i=0; i<$n; i++ )) do for (( j=0; j<$n; j++ )) do if [ ${a[$i]} -gt ${a[$j]} ] then t=${a[$i]} a[$i]=${a[$j]} a[$j]=$t fi done done echo The elements after sorting are for (( i=0; i<$n; i++ )) do printf "%3d" ${a[$i]} done echo sleep 2 OUTPUT -----------[y65532@aditya unix1]$ sh 16 Enter the array size 5
22
The elements before sorting are 1 2 8 6 4 The elements after sorting are 8 6 4 2 1
23
17. Write a Shell program to display the digits, which are in the odd Positions in a given five-digit number. SCRIPT ---------#!/bin/bash echo Enter any five digited number read n count=0 t=$n while [ $t -gt 0 ] do count=`expr $count + 1` t=`expr $t / 10` done if [ $count -ne 5 ] then echo The entered number is not a five digited number fi i=0 while [ $n -gt 0 ] do r=`expr $n % 10` a[$i]=$r i=`expr $i + 1` n=`expr $n / 10` done for (( i=5; i>=0; i-- )) sdo if [ `(expr $i % 2)` -eq 0 ] then printf "%3d" ${a[$i]} fi done sleep 2 OUTPUT -----------[y65532@aditya unix1]$ sh 17 Enter any five digited number 12445 145
24
18. Write a Shell script to find the largest number among the three given numbers. SCRIPT ----------#!/bin/bash echo Enter three numbers read a b c if [ $a -gt $b -a $a -gt $c ] then echo $a is the greatest number elif [ $b -gt $c -a $b -gt $a] then echo $b is the greatest number else echo $c is the greatest number fi sleep 2 OUTPUT -----------[y65532@aditya unix1]$ sh 18a Enter three numbers 891 9 is the greatest number
25
19. Write a Shell program to search for a given number from a list of Numbers provided using binary search method. SCRIPT ---------#!/bin/bash echo enter array size read n echo enter array elements for (( i=0;i<$n;i++ )) do read a[$i] done echo Enter element to be searched: read ele flag=0 low=0 high=`expr $n - 1` while [ $low -le $high ] do mid=`expr $low + $high` mid=`expr $mid / 2` if [ ${a[$mid]} -eq $ele ] then flag=1 break fi if [ ${a[$mid]} -lt $ele ] then high=`expr $mid - 1` fi if [ ${a[$mid]} -gt $ele ] then low=`expr $mid + 1` fi done if [ $flag -eq 1 ] then echo The element is found in the array else echo The element is not found fi sleep 2
26
OUTPUT -----------[y65532@aditya unix1]$ s 19 bash: s: command not found [y65532@aditya unix1]$ sh 19 enter array size 4 enter array elements 1 5 8 2 Enter element to be searched: 5 The element is found in the array
27
20. Write grep command to find each line of the given file that contains some required pattern and display the number of occurrences. SCRIPT ---------[y65532@aditya unix1]$ grep -c echo 13 3
28
21. Create a file with delimiter. First name, last name, DOB, address,pincode. SCRIPT ---------#!/bin/sh echo Enter the first name read fname echo Enter the last name read lname echo Enter the date of birth read dob echo Enter the address read add echo Enter the pincode read pin echo Enter the delimiter read del echo The output with delimiter is: echo $fname $del $lname $del $dob $del $add $del $pin >add cat add sleep 2 OUTPUT ----------[y65532@aditya unix1]$ sh 21 Enter the first name hari Enter the last name kishore Enter the date of birth 8-12-1985 Enter the address vijayawada Enter the pincode 520001 Enter the delimiter ::: The output with delimiter is: hari ::: kishore ::: 8-12-1985 ::: vijayawada ::: 520001
22. Write script that will ask user, fullname (first, middle, last name) greet user by first name. Ask users DOB and calculate users age, print users home directory.
29
SCRIPT ---------#!/bin/sh echo Enter the first name read first echo Enter the middle name read middle echo Enter the last name read last echo "Hai!" $first echo Enter the date of birth year read dob echo The present year is d=`date | cut -d " " -f6` echo $d age=`expr $d - $dob` echo The age is: $age echo The home directory is $HOME sleep 2 OUTPUT ----------[y65532@aditya unix1]$ sh 22 Enter the first name hari Enter the middle name kishore Enter the last name g Hai! hari Enter the date of birth year 1985 The present year is 2006 The age is: 21 The home directory is /home/mcai/y65532
23. Change all occurrences of "P.B.Siddhartha College of Arts and Science" to "P.G.Centre of P.B.Siddhartha" present in a file. SCRIPT
30
---------#!/bin/sh echo Enter the file name read f echo Contents of the file cat $f echo echo Contents after replacement sed 's/P.B.Siddhartha/P.G. Center of Siddhartha/g' $f OUTPUT -----------[y65532@aditya unix1]$ sh 23 Enter the file name temp Contents of the file Welcome to P.B.Siddhartha College P.B.Siddhartha College P.G.Centre Contents after replacement Welcome to P.G. Center of Siddhartha College P.G. Center of Siddhartha College P.G.Centre
24. Replace all occurrences of " CSI and P.B.Siddhartha" with "P.B.Siddhartha and CSI". SCRIPT ---------31
#!/bin/sh echo Enter the file name read f echo Contents of the file cat $f echo echo Contents after replacement cat $f|sed 's/P.B.Siddhartha and CSI/CSI and P.B.Siddhartha/g' > $f cat $f
25. Write an awk script, which will return the factorial of a user supplied number. SCRIPT ---------{
32
a=$0 f=1; for(i=2;i<=a;i++) f=f*i; print "Factorial is " f } OUTPUT -----------[y65532@aditya unix1]$ awk -f 25 6 (Press ctr + d) Factorial is 720 or [y65532@aditya unix1]$ awk -f 25 25input Factorial is 720 Factorial is 5040 Factorial is 40320
26. Create a script that sleeps for 10 minutes. Excute the script so that it is running ever after user has logged out. Determine the process id of that script and kill it. SCRIPT ---------# contents in sleepscript.sh file are as follows:
33
sleep 10m main script -----------#!/bin/sh # Script that runs a script that sleeps for 10 minutes in background # And then gets pid for that process to kill it ./sleepscript.sh & echo $! >pid echo process id to be killed `cat pid`... press enter read k=`cat pid` echo killing $k... echo `ps -ae|grep $k` kill $k echo done OUTPUT -----------[y65532@aditya lab]$ sh lab26.sh process id to be killed 24004... press enter killing 24004... 24004 pts/2 00:00:00 sh done
27. Print out a report of users who have accounts on the system and their login shell. SCRIPT ---------#!/bin/bash echo The users who have account on the system and their login shells are: cat /etc/passwd | cut -f1,7 -d: | more -d
34
sleep 2 OUTPUT ----------[y65532@aditya lab]$ sh lab27.sh The users who have account on the system and their login shells are: root:/bin/bash bin:/sbin/nologin daemon:/sbin/nologin adm:/sbin/nologin lp:/sbin/nologin sync:/bin/sync shutdown:/sbin/shutdown halt:/sbin/halt mail:/sbin/nologin news: uucp:/sbin/nologin operator:/sbin/nologin games:/sbin/nologin gopher:/sbin/nologin ftp:/sbin/nologin nobody:/sbin/nologin
28. Write a shell script that will take non-zero (variable) command line arguments and print them in reverse order. Input myscript arg1 arg2 ... arg5 Output arg5 arg4 ... arg1 SCRIPT: ---------35
#!/bin/bash str="" while [ $# -gt 0 ] do str=`echo $1 $str` shift done echo $str sleep 2 OUTPUT: -----------[y65532@aditya lab]$ sh lab28.sh arg1 arg2 2 3 4 a b b a 4 3 2 arg2 arg1
29. Write a shell script that will compute the value raised to a power Input : power m n Output : m raised to the power of n Note: m raised to the power of 0 is 1 SCRIPT: ----------#!/bin/bash if [ $# -ne 2 ]
36
then echo Invalid number of arguments exit fi b=$1 sum=1 for ((i=0; i<$2; i++ )) do sum=`expr $sum \* $b` done echo The exponential value is: $sum sleep 2 OUTPUT: ----------[y65532@aditya lab]$ sh lab29.sh 3 5 The exponential value is: 243 [y65532@aditya lab]$ sh lab29.sh Invalid number of arguments [y65532@aditya lab]$ sh lab29.sh 1 0 The exponential value is: 1
30. Write a shell script convert that will convert a given constant in a specified base into the specified base using functions. Input Format: convert number currentbase newbase Example: convert A1034 16 8 Output for above example: 02410064 SCRIPT: ----------37
#!/bin/sh # Shell script to convert a given constant from specified base to specified base usage() { # function that prints usage aand exits echo Invalid arguments echo Usage: $0 number from-base to-base echo Maximum base supported is 16, minimum is 2 echo " " exit } digittonum() { # function that converts characters in a given base (upto 16) to decimal equival ent if [ $digit = A ] ; then digit=10 elif [ $digit = B ] ; then digit=11 elif [ $digit = C ] ; then digit=12 elif [ $digit = D ] ; then digit=13 elif [ $digit = E ] ; then digit=14 elif [ $digit = F ] ; then digit=15 fi } numtodigit() { # function that converts a number to its base represented character (upto base 1 if [ $digit -eq 10 ] ; then digit=A elif [ $digit -eq 11 ] ; then digit=B elif [ $digit -eq 12 ] ; then digit=C elif [ $digit -eq 13 ] ; then digit=D elif [ $digit -eq 14 ] ; then digit=E elif [ $digit -eq 15 ] ; then digit=F fi }
38
converttodec() { # function that converts $number from base $2 to its decimal equivalent pow=1 pos=`echo $number|wc -c` pos=`expr $pos - 1` sum=0 while [ $pos -ne 0 ] ; do digit=`echo $number|cut -c $pos` pos=`expr $pos - 1` number=`echo $number|cut -c 1-$pos` digittonum mul=`expr $digit \* $pow` sum=`expr $sum + $mul` pow=`expr $pow \* $from_base` done base_10=$sum } converttorequired() { # function that converts $base_10 to its required base echo to convert $base_10 from base 10 to base $to_base number=$base_10 sum="" while [ $number -ge $to_base ] ; do digit=`expr $number % $to_base` numtodigit sum=$digit$sum number=`expr $number / $to_base` done base_n=$number$sum } if [ $# -ne 3 ] ; then usage fi if [ $2 -lt 2 -o $2 -gt 16 ] ; then usage fi if [ $3 -lt 2 -o $3 -gt 16 ] ; then usage fi number=$1 from_base=$2 to_base=$3 base_10=0
39
base_n=0 converttodec echo Intermediate result: Equivalent of $1 in base $2 is $base_10 in base 10 converttorequired echo Final result is: $base_n echo OUTPUT: ----------[y65532@aditya unix]$ sh numconvert.sh A1034 16 8 Intermediate result: Equivalent of A1034 in base 16 is 659508 in base 10 to convert 659508 from base 10 to base 8 Final result is: 2410064
31. Calculate the compound interest where the amount, interest rate and time are specified Input: compinterest amount interest time SCRIPT: ---------#!/bin/bash if [ $# -ne 3 ] then echo Invalid number of arguments fi
40
p=$1 ra=$2 n=$3 br=`echo $ra / 100 | bc -l` br=`echo $br + 1 | bc -l` pow=1 for ((i=1; i<=$n; i++)) do pow=`echo $pow \* $br|bc -l` done ci=`echo $p \* $pow | bc -l` echo The compound interest is: `echo $ci - $p|bc -l` sleep 2 OUTPUT: -----------[y65532@aditya lab]$ sh lab31.sh 1000 2 33 The compound interest is: 922.23140394315183157000
32. Identify the top 10 disk usage files in /usr/bin [y65532@aditya bin]$ du * | sort -rn |head 4244 xemacs-21.1.14 3440 doxygen 3428 emacs-20.7 3428 emacs 3360 gs 3232 ddd 3228 emacs-nox 3068 pine
41
2552 2068
jikes nwadmin
33. Write a shell script that determines if a given input (numeric and character string) is a palindrome. SCRIPT: ----------#!/bin/sh echo Enter any name or number: read str rstr=`echo $str | rev` if [ $str = $rstr ] then
42
echo The given item is palindrome else echo The given item is not palindrome fi sleep 2 OUTPUT: -----------[y65532@aditya lab]$ sh lab33.sh Enter any name or number: 121 The given item is palindrome [y65532@aditya lab]$ sh lab33.sh Enter any name or number: malayalam The given item is palindrome Enter any name or number: abcd The given item is not palindrome
34. Write a script to find the largest value from among a set of values in an input file and delete it from an array. Print out the values and the non-zero frequency of each item. SCRIPT: ----------#!/bin/sh # To copy contents of file into array and display array echo echo The elements in the file are: n=0 for i in `cat temp`
43
do a[$n]=$i printf "%3d" ${a[$n]} let n++ done echo # To find the maximum of the array max=0 for((i=0; i<n; i++)) do if [ $max -lt ${a[$i]} ] then max=${a[$i]} fi done echo echo The maximum element in the file is: $max # To remove the maximum element from the array for((i=0; i<n; i++)) do if [ $max -eq ${a[$i]} ] then for((k=$i; k<n; k++)) do a[$k]=${a[$k+1]} done n=`expr $n - 1` i=`expr $i - 1` fi done # To check whether the max is the only element in the array if [ $n -eq 0 ] then echo There are no elements in the array exit fi # To display array elements after deleting max echo echo Array elements after deleting is: for((i=0; i<n; i++)) do printf "%3d" ${a[$i]}
44
done echo # To create an array t to store frequencies for((k=0; k<max; k++)) do t[$k]=0 done # To update the frequency of array elements in array t for((i=0; i<n; i++)) do t[${a[$i]}]=`expr ${t[${a[$i]}]} + 1` done # To display the frequency of the non-zero frequency of eac echo echo The frequency of elements is: for((i=0; i<max; i++)) do if [ ${t[$i]} -gt 0 ] then echo The frequency of $i is: ${t[$i]} fi done sleep 2 OUTPUT: -----------[y65532@aditya lab]$ sh lab34.sh The elements in the file are: 5 6 7 8 5 6 4 The maximum element in the file is: 8 Array elements after deleting is: 5 6 7 5 6 4 The frequency of elements is: The frequency of 4 is: 1 The frequency of 5 is: 2 The frequency of 6 is: 2 The frequency of 7 is: 1
45
35. Write a script to calculate the age of a user to the nearest hour. SCRIPT: ----------{ print $1 ", your date of birth is " $2 btimestamp=mktime($2); print "Timestamp for your birthday is " btimestamp stimestamp=systime(); print "Current system timestamp is " stimestamp dtimestamp=stimestamp-btimestamp; print "Difference in timestamps is " dtimestamp
46
years=strftime("%Y",dtimestamp); years=years-1970 print "Your are " years " Years " strftime("%m",dtimestamp) " Months "st rftime("%d",dtimestamp) " Days " strftime("%H",dtimestamp) " Hours "strftime("%M ",dtimestamp) " Minutes " strftime("%S",dtimestamp) " Seconds old (and counting. ..)" } OUTPUT: -----------[y65532@aditya y65532]$ awk -F ':' -f 35 Hari Kishore:1985 08 02 14 05 30 Hari Kishore, your date of birth is 1985 08 02 14 05 30 Timestamp for your birthday is 491819730 Current system timestamp is 1162374279 Difference in timestamps is 670554549 Your are 21 Years 04 Months 02 Days 06 Hours 39 Minutes 09 Seconds old (and coun ting...) or [y65532@aditya y65532]$ awk -F ':' -f 35 35input Hari Kishore, your date of birth is 1985 02 08 14 05 30 Timestamp for your birthday is 476699730 Current system timestamp is 1162374208 Difference in timestamps is 685674478 Your are 21 Years 09 Months 24 Days 06 Hours 37 Minutes 58 Seconds old (and counting...)
36. Read in input file name and output file name and convert the Characters into inverse case. [y65532@aditya lab]$ cat lab27.sh | tr [a-zA-Z] [A-Za-z] >output [y65532@aditya lab]$ cat output #!/BIN/BASH ECHO tHE USERS WHO HAVE ACCOUNT ON THE SYSTEM ARE:
47
37. Write a shell program to concatenate the contents of two files Without using the cat command. SCRIPT: ---------#!/bin/sh echo Enter the first filename: read f1 echo Enter the second filename: read f2 l1=`wc -l $f1|tr -s " "|cut -d" " -f2` head -$l1 $f1 >temp l2=`wc -l $f2|tr -s " "|cut -d" " -f2` head -$l2 $f2 >>temp
48
cat temp OUTPUT: ----------[y65532@aditya lab]$ sh lab38.sh Enter the first filename: items Enter the second filename: pid Carrots 5 14.00 Eggs 12 1.25 Bread 6 12.00 24004
38. Display username and idle time for each user who is logged in. [y65532@aditya lab]$ who --idle | tr -s " "|cut -f1,6 -d" " y55552 01:33 y65532 . y55731 00:03 y55541 00:03 y55555 . y55506 01:02
49
39. Write a shell script to reverse a given number Input 23619 Output 91632 SCRIPT: ----------#!/bin/bash echo Enter any number: read n rev=0 while [ $n -ne 0 ] do ra=`expr $n % 10` rev=`expr $rev \* 10 + $ra`
50
n=`expr $n / 10` done echo The reverse of given number is: $rev sleep 2 OUTPUT: -----------[y65532@aditya lab]$ sh lab40.sh Enter any number: 23619 The reverse of given number is: 91632
40. Write a shell script that calls a function fact that will compute the factorial of a specified input SCRIPT: ---------#!/bin/sh factorial() { fact=1 while [ $n -gt 0 ] do fact=`expr $fact \* $n` n=`expr $n - 1` done }
51
echo Enter any number: read n factorial $n echo The factorial of given number is: $fact sleep OUTPUT: ------------[y65532@aditya lab]$ sh lab41.sh Enter any number: 5 The factorial of given number is: 120
52