cooltoad51

Untitled

Nov 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.77 KB | None | 0 0
  1. 2
  2.  
  3.  
  4.  
  5. Day 1
  6.  
  7.  
  8. passwd, who, tty, script, clear and tput, uname, date, cal, bc, man, pwd, mkdir, cd, rmdir
  9.  
  10.  
  11. Lab Assignments:
  12.  
  13. 1. Perform the following
  14.  
  15. a) List the names of the users logged in and their total count without displaying any further details. b) Find out your terminal‟s device name.
  16. c) Display current date in the form dd/mm/yyyy.
  17. d) Find out your machine‟s name and the version of the operating system.
  18. e) Clear the screen and place the cursor at row 12, column 25. f) Find the decimal equivalent of 1101001.
  19. g) Find out the users who are idling. h) Use man to get help
  20. i) Ensure that bc displays the results of all divisions using three decimal places.
  21.  
  22.  
  23.  
  24. Solutions:
  25.  
  26. 1)a) $ who -q b) $ tty
  27. c) $ date +% d/%m/%Y
  28. d) $ uname -nr e) $ clear
  29. $ tput cup 12 25
  30.  
  31. f) $ bc ibase=2
  32. g) $ who -Hu h) $ man tty
  33.  
  34. I) $ bc
  35. Scale=3
  36. 3
  37.  
  38.  
  39.  
  40.  
  41. Day 2
  42.  
  43. cp, rm, mv, cat, file, ls, umask, chmod, touch, ln, find
  44.  
  45. Lab Assignments:
  46.  
  47. 1. Perform the following:
  48.  
  49. a) Create a directory structure in your home directory (mca, two subdirectories cprogs and projects
  50. under mca) while being in your home directory. b) Change to the directory projects.
  51. c) Create a file called biodata and store your name, age, sex, and address in it.
  52. d) Make a copy of the file biodata into another file text within the directory cprogs. e) Move the file text from cprogs to projects.
  53. f) Combine the contents of the file biodata and text into another file datatext. g) Rename the file text to newtext.
  54. h) Change the permissions of the file newtext to rw-rw-rw-. i) List all filenames starting with „a‟ or „b‟ or „m‟.
  55. j) List all filenames that end with a digit.
  56. k) List all files in the current directory whose second character is a digit.
  57. l) Use command(s) to create a directory in your home directory called KeepOut whose contents can be read only by you.
  58.  
  59.  
  60.  
  61. Solution:
  62.  
  63. 1. a) $ mkdir mca mca/cprogs mca/projects b) $ cd /mca/projects
  64. c) $ cat > /mca/projects/biodata
  65. ( write some datas)
  66.  
  67. d) $ cp /mca/projects/biodata /mca/cprogs/text e) $ mv /mca/cprogs/text /mca/projects/text
  68. f) $ cat /mca/projects/biodata /mca/cprogs/text >> /mca/projects/datatext g) $ mv /mca/projects/text /mca/projects/newtext
  69. h) $ chmod 666 /mca/projects/newtext
  70.  
  71. I) $ ls [abm] * J) $ ls * [0-9]
  72. K) $ ls ? [0-9]*
  73.  
  74. L) $ mkdir keepout ; chmod 400 keepout
  75. 4
  76.  
  77.  
  78.  
  79.  
  80. Day 3
  81.  
  82. wc, od, cmp, diff, comm., tee, head, tail, cut, paste, sort, tr, uniq
  83.  
  84. Lab Assignments:
  85.  
  86. 1. Perform the following:
  87.  
  88. a) List all files beginning with character „a‟ on the screen and also store them in a file called file1.
  89. b) Sort the output of who and display on screen along with total number of users. The same output except the number of users should be stored in a file file1.
  90. c) Double space a file
  91. d) Select lines 5 to 10 of a file
  92. e) Find the user name and group id from the file /etc/passwd using the cut command. f) Extract the names of the users from /etc/passwd after ignoring the first 10 entries.
  93. g) Sort the file /etc/passwd on GUID (primary) and UID (secondary) so that the users with the same
  94. GUID are placed together. User with a lower UID should be placed higher in the list. h) List from /etc/passwd the UID and the user having the highest UID.
  95. i) Device a sequence which lists the five largest files in the current directory.
  96. j) Remove duplicate lines from a file.
  97. k) Count the frequency of occurrences of words in a file.
  98. l) Find "long" listing of the smallest 5 files in the /etc directory whose name contains the string ".conf", sorted by increasing file size.
  99. m) What would you type at the command line to get a sorted list, with no duplicates, of all the users
  100. logged into the local network?
  101. n) What would you type at the command line to find all files in your home directory that are more than a week old and end with .bak?
  102. What would you type at the command line to find out how many total lines are contained in all the
  103. files ending in .c in the current directory, printing only the total number of lines?
  104.  
  105.  
  106.  
  107. Solution:
  108.  
  109. 1. a) $ ls [a] * | tee file1
  110.  
  111. b) $ who -q | sort ; who -Hu | cat >> file1 c) $ pr -d file1
  112. d) $ head -10 file1 | tail -n -5
  113.  
  114. e) $ cut -d “:” -f 1,4 /etc/passwd
  115.  
  116. f) $ cut -d “:” -f 1 /etc/passwd | tail -n +11 g) $ cut -d “:” -f 3,4 /etc/passwd | sort -n
  117. h) $ sort -t “:” -r -n -k 3 /etc/passwd | cut -d “:” -f 1,3 | head -1 i) $ ls -lS | head -6
  118. j) $ uniq file1
  119.  
  120. k) $ sort file1 | uniq -c
  121. 5
  122.  
  123.  
  124.  
  125.  
  126. l) $ ls -lSr /etc/*.conf | head -5 m) $ who | uniq |sort
  127. n) $ find -mtime +7 -name “*.bak” -ls o) $ wc -l *.c
  128. 6
  129.  
  130.  
  131.  
  132.  
  133. Day 4
  134.  
  135. ps, kill, grep, egrep, fgrep
  136.  
  137. Lab Assignments:
  138.  
  139. 1. Perform the following
  140.  
  141. a) Find out the PID of your login shell.
  142. b) Remove the header line from the ps output.
  143. c) List all processes that you are currently running on your machine, sorted by the command name in alphabetical order (i.e. a process running acroread should be listed before a process running zwgc). The
  144. output should consist only of the processes you are running and nothing else (i.e. if you are running 6 processes, the output should only have 6 lines).
  145. d) Display the files in the current directory that contain the string MCA HITK in either upper- or lowercase.
  146. e) Store in a variable the number of lines containing the word MCA in the files mca1, mca2 and mca3.
  147. f) If you did not have the wc command, how would you use grep to count the number of users currently using the system?
  148. g) Remove blank lines from a file using grep.
  149. h) List the ordinary files in your current directory that are not writable by the owner.
  150. i) Locate lines ending and beginning with a dot and containing anything between them. j) Locate lines that are less than 100 characters in length.
  151.  
  152.  
  153.  
  154.  
  155.  
  156. Solution:
  157. 1. (a to c is discarded)
  158.  
  159. d) $ grep -il „MCAHITK‟ *
  160.  
  161. e) $ var=`grep MCA mca[1-3] | wc -l`
  162.  
  163. $ echo $var
  164.  
  165. f) who | grep -c “.*”
  166.  
  167. g) $ grep -v “^$” aa1
  168.  
  169. h) $ ls -l | grep -v „^..w‟
  170.  
  171. i) $ grep „^\..*\.$‟ mca4
  172.  
  173. j) $ grep „^.\{0,99\}$‟ * file1
  174. 7
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. Day 5
  182.  
  183. Lab Assignments:
  184. 1. Write a shell script to find out whether an integer input through the keyboard is an odd number or an even number.
  185. 2. Write a shell script to find out whether any year input through the keyboard is a leap year or not. If no argument is supplied the current year should be assumed.
  186. 3. Write a shell script to find the maximum of three numbers provided as command line arguments.
  187. 4. Write a shell script to check whether a given number is prime or not.
  188.  
  189.  
  190. Solution: (LAB)
  191.  
  192. 1) #! /bin/sh
  193.  
  194. # Purpose: To find whether an integer taken as input is an odd or even number.
  195.  
  196. # USAGE: sh oddeven.sh (No arguments required)
  197.  
  198. echo “Enter an integer:”
  199.  
  200. read n
  201.  
  202. if test `expr $n % 2` -eq 0 then
  203. echo “$n is a even no.”
  204.  
  205. else
  206.  
  207. echo “$n is a odd no.”
  208.  
  209. fi
  210.  
  211. # end of script
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. 2) #! /bin/sh
  222.  
  223. # Purpose: To find out whether a year taken as input is leap year or not.
  224.  
  225. # USAGE: sh leapyear.sh year
  226.  
  227. If test $# -eq 0 then
  228. 8
  229.  
  230.  
  231.  
  232.  
  233. set `date`
  234.  
  235. var=$6
  236.  
  237. else
  238.  
  239. var=$1
  240.  
  241. fi
  242.  
  243. if test `expr $var % 4` -eq 0 -a `expr $var % 100` -ne 0 -o `expr $var % 400` -eq 0`
  244.  
  245. then
  246.  
  247. echo “$var is a leap year”
  248.  
  249. else
  250.  
  251. echo “$var is not a leap year”
  252.  
  253. fi
  254.  
  255. # end of script
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262. 3) #! /bin/sh
  263.  
  264. # Purpose: To find out maximum of three numbers taken as command line argument.
  265.  
  266. # USAGE: sh maxthreeno.sh number1 number2 number3
  267.  
  268. If test $# -ne 3 then
  269. echo “please give three numbers”
  270.  
  271. exit fi
  272. if test $# -eq 3 then
  273. max=$1
  274.  
  275. if test $2 -gt $max then
  276. max=$2
  277. 9
  278.  
  279.  
  280.  
  281.  
  282. fi
  283.  
  284. if test $3 -gt $max then
  285. max=$3
  286.  
  287. fi
  288.  
  289. fi
  290.  
  291. echo “max number=$max”
  292.  
  293. #end of script
  294.  
  295.  
  296.  
  297.  
  298. 4) #! /bin/sh
  299.  
  300. # Purpose: To check whether a integer is prime or not taken as input.
  301.  
  302. # USAGE: sh primecheck.sh(No arguments required)
  303.  
  304. echo “Enter a number:”
  305.  
  306. read n i=1
  307. k=0
  308.  
  309. while test $i -le $n do
  310. if test `expr $n % $i ` -eq 0 then
  311. k=`expr $k + 1`
  312.  
  313. fi
  314.  
  315. i=`expr $i + 1`
  316.  
  317. done
  318.  
  319. if test $k -eq 2 then
  320. echo “$n is prime”
  321.  
  322. else
  323. 10
  324.  
  325.  
  326.  
  327.  
  328. echo “$n is not prime”
  329.  
  330. fi
  331.  
  332. # en d of script
  333.  
  334.  
  335.  
  336.  
  337. Home assignments:
  338. 1. Write a shell script to find the factorial value of any integer entered through the keyboard.
  339. 2. Write a shell script to generate all combinations of 1, 2 and 3.
  340. 3. Write a shell script to print all prime numbers in a given range.
  341. 4. Write a shell script to calculate the sum of digits of any number entered through keyboard.
  342. 5. Rajesh‟s basic salary (BASIC) is input through the keyboard. His dearness allowance (DA) is 52% of BASIC. House rent allowance (HRA) is 15% of BASIC. Contributory provident fund is 12% of (BASIC + DA). Write a shell script to calculate his gross salary and take home salary using the following formula:
  343. Gross salary = BASIC + DA + HRA
  344.  
  345. Take home salary = Gross salary - (BASIC + DA) * 0.12
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352. Solution:(HOME)
  353.  
  354. 1) #! /bin/sh
  355.  
  356. #Purpose: To find out the factorial of a given integer.
  357.  
  358. # USAGE: sh factorial.sh (No arguments required)
  359.  
  360. echo “Enter an integer:”
  361.  
  362. read n f=1
  363. i=1
  364.  
  365. while test $i -le $n do
  366. f=`expr $f \* $i`
  367.  
  368. i=`expr $i + 1`
  369.  
  370. done
  371.  
  372. echo “Factorial value: $f”
  373.  
  374. # end of script
  375. 11
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382. 2) #! /bin/sh
  383.  
  384. #Purpose: To generate all combinations of 1,2 and 3.
  385.  
  386. # USAGE: sh combin123.sh (no arguments required)
  387.  
  388. echo “Respective combination of 1,2,3 are:”
  389.  
  390. i=1
  391.  
  392. while test $i -le 3 do
  393. j=1
  394.  
  395. while test $j -le 3 do
  396. k=1
  397.  
  398. while test $k -le 3 do
  399. s=`expr $i \* 100 + $j \* 10 + $k`
  400.  
  401. echo $s
  402.  
  403. k=`expr $k + 1`
  404.  
  405. done
  406.  
  407. j=`expr $j + 1`
  408.  
  409. done
  410.  
  411. i=`expr $i + 1`
  412.  
  413. done
  414.  
  415. #end of script
  416.  
  417.  
  418.  
  419.  
  420. 3) #! /bin/sh
  421.  
  422. # Purpose: To print all prime numbers in a given range.
  423.  
  424. # sh primerange.sh (No arguments required)
  425.  
  426. echo “Enter the range:”
  427. 12
  428.  
  429.  
  430.  
  431.  
  432. read n i=1
  433. while test $i -le $n do
  434. b=1 c=0
  435. while test $b -le $i do
  436. if test `expr $i % $b` -eq 0 then
  437. c=`expr $c + 1`
  438.  
  439. fi
  440.  
  441. b=`expr $b + 1`
  442.  
  443. done
  444.  
  445. if test $c -eq 2 then
  446. echo “$i is a prime number”
  447.  
  448. fi
  449.  
  450. i=`expr $i + 1`
  451.  
  452. done
  453.  
  454. #end of script
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464. 4) #! /bin/sh
  465.  
  466. # Purpose: To find out the sum of digits given as input.
  467.  
  468. # USAGE: sh sumdigits.sh (No arguments required)
  469.  
  470. echo “Enter a number :”
  471. 13
  472.  
  473.  
  474.  
  475.  
  476. read n sum=0
  477. while test $n -gt 0 do
  478. r=`expr $n % 10`
  479.  
  480. sum=`expr $sum + $r`
  481.  
  482. n=`expr $n / 10`
  483.  
  484. done
  485.  
  486. echo “Sum of digits:$sum”
  487.  
  488. # end of script
  489.  
  490.  
  491.  
  492.  
  493. 5) #! /bin/sh
  494.  
  495. #Purpose: To find out the Basic salary of an employee.
  496.  
  497. # USAGE: sh basicsalary.sh (no arguments required)
  498.  
  499. echo “Enter rajesh `s basic salary:”
  500.  
  501. read basic
  502.  
  503. da=`expr $basic \* 52 / 100` hra=`expr $basic \* 15 / 100` v=`expr $basic + $da` cpf=`expr $v \* 12 / 100` gs=`expr $basic + $da + $hra` hs=`expr $gs - $cpf`
  504. echo “Gross salary:$gs”
  505.  
  506. echo “Home salary:$hs”
  507.  
  508. #end of script
  509. 4
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518. Day 6
  519.  
  520.  
  521.  
  522. Lab Assignments:
  523. 1. Write a shell program that takes a number from user and prints the reverse of the number.
  524. 2. Write a shell script to determine whether two numbers input through keyboard are prime to each other.
  525. 3. Write a shell script to find whether a number is divisible by 11.
  526. 4. Write a shell script that produces a shell calculator to perform the following operations:
  527. 1. Addition
  528. 2. Subtraction
  529. 3. Multiplication
  530. 4. Division
  531.  
  532.  
  533.  
  534.  
  535.  
  536. Solution: (LAB)
  537.  
  538. 1) #! /bin/sh
  539.  
  540. # Purpose: To find out the reverse of a given number.
  541.  
  542. # USAGE: sh reverseno.sh (no arguments required)
  543.  
  544. echo “ Enter a number:”
  545.  
  546. read n sum=0
  547. while test $n -gt 0 do
  548. r=`expr $n % 10`
  549.  
  550. sum=`expr $sum \* 10 + $r`
  551.  
  552. n=`expr $n / 10`
  553.  
  554. done
  555.  
  556. echo “Reverse of a number:$sum”
  557.  
  558. #end of script
  559.  
  560.  
  561.  
  562.  
  563. 2) #! /bin/sh
  564. 15
  565.  
  566.  
  567.  
  568.  
  569. #Purpose: To determine whether two numbers taken as input are prime to each other.
  570.  
  571. # USAGE: sh primeeachother.sh (NO arguments required)
  572.  
  573. echo “Enter first number:”
  574.  
  575. read a
  576.  
  577. echo “Enter second number:”
  578.  
  579. read b
  580.  
  581. if test $b -gt $a then
  582. max=$b min=$a
  583. else
  584.  
  585. max=$a min=$b
  586. fi
  587.  
  588. r=`expr $max % $min`
  589.  
  590. while test $r -ne 0 do
  591. max=$min min=$r
  592. r=`expr $max % $min`
  593.  
  594. done
  595.  
  596. if test $min -eq 1 then
  597. echo “$a and $b are prime to each other”
  598.  
  599. else
  600.  
  601. echo “$a and $b are not prime to each other”
  602.  
  603. fi
  604.  
  605. #end of script
  606. 16
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618. 3) #! /bin/sh
  619.  
  620. #Purpose: To check whether the given number is divisible by 11.
  621.  
  622. # USAGE: sh div11.sh (no arguments required)
  623.  
  624. echo “Enter a number:”
  625.  
  626. read n p=0
  627. c=0 k=0
  628. while test $n -gt 0 do
  629. r=`expr $n % 10` c=`expr $c + 1` n=`expr $n / 10`
  630. done i=1
  631. while test $i -le $c do
  632. r=`expr $n % 10`
  633.  
  634. if test `expr $i % 2` -ne 0 then
  635. p=`expr $p + 1`
  636.  
  637. else
  638.  
  639. k=`expr $k + 1`
  640.  
  641. fi
  642.  
  643. n=`expr $n / 10`
  644. 17
  645.  
  646.  
  647.  
  648.  
  649. i=`expr $i + 1`
  650.  
  651. done
  652.  
  653. if test $k -gt $p then
  654. l=`expr $k - $p`
  655.  
  656. else
  657.  
  658. l=`expr $p - $k`
  659.  
  660. fi
  661.  
  662. if test `expr $l % 11` -eq 0 then
  663. echo “Divisible by 11”
  664.  
  665. else
  666.  
  667. echo “Not divisible by 11”
  668.  
  669. fi
  670.  
  671. #end of script
  672.  
  673.  
  674.  
  675.  
  676. 4) #! /bin/sh
  677.  
  678. #Purpose: A program to generate a calculator which performs basic arithmetical operations.
  679.  
  680. # USAGE: sh calculator.sh operand operator operand
  681.  
  682. If test $# -ne 3 then
  683. echo “Please give three arguments as operand op operand”
  684.  
  685. exit fi
  686. case $2 in
  687.  
  688. +) var=`expr $1 + $ 3`;;
  689.  
  690. -)var=`expr $1 - $3`;;
  691.  
  692. X)var=`expr $1 \* $3`;; #(PLEASE GIVE X INSTEAD OF * DURING MULTIPLICATION)
  693. 18
  694.  
  695.  
  696.  
  697.  
  698. /)var=`expr $1 / $3`;;
  699.  
  700. *) echo “Invalid operator”;;
  701.  
  702. esac
  703.  
  704. echo “The required value:$var”
  705.  
  706. #end of script
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713. Home assignments:
  714.  
  715. 1. Write a shell script to print the following pattern for any number of lines:
  716.  
  717.  
  718. *
  719.  
  720. * * *
  721.  
  722. * * * * *
  723.  
  724. * * * * * * *
  725.  
  726. * * * * * * * * *
  727.  
  728.  
  729.  
  730.  
  731. 2. Write a shell script to test whether a given string is palindrome or not.
  732. 3. Write a shell script which counts the number of consonants and vowels in a given sentence.
  733. 4. Write a shell script to display the list of users as well as the number of users connected to the
  734. system.
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741. Solution: (HOME)
  742.  
  743. 1) #! /bin/sh
  744.  
  745. #Purpose: To generate a pattern with stars.
  746.  
  747. # USAGE: sh pattern.sh (no argments required)
  748.  
  749. echo “Enter the number of rows:”
  750.  
  751. read r i=1
  752. while test $i -le $r
  753. 19
  754.  
  755.  
  756.  
  757.  
  758. do
  759.  
  760. k=`expr $r - $i` while test $k -gt 0 do
  761. echo -n “2blanks”
  762.  
  763. k=`expr $k - 1`
  764.  
  765. done j=1
  766. while test $j -le `expr 2 \* i - 1`
  767.  
  768. do
  769.  
  770. echo -n “*blank”
  771.  
  772. j=`expr $j + 1`
  773.  
  774. done echo
  775. i=`expr $i + 1`
  776.  
  777. done
  778.  
  779. #end of script
  780.  
  781.  
  782.  
  783.  
  784. 2) #! /bin/sh
  785.  
  786. #Purpose:To check whether the given string is palindrome or not.
  787.  
  788. # USAGE: sh palindromestring.sh (no arguments required)
  789.  
  790. echo “Enter a string:”
  791.  
  792. read n len=${#n}
  793. v=`expr $len / 2 + 1`
  794.  
  795. i=1
  796.  
  797. while test $i -lt $v do
  798. 20
  799.  
  800.  
  801.  
  802.  
  803. k1=`echo $n| cut -c $i` k2=`echo $n|cut -c $len` if test “$k1” != “$k2” then
  804. echo “Not palindrome”
  805.  
  806. exit fi
  807. len=`expr $len - 1`
  808.  
  809. i=`expr $i + 1`
  810.  
  811. done
  812.  
  813. echo “Palindrome “
  814.  
  815. #end of script
  816.  
  817.  
  818.  
  819.  
  820. 3) #! /bin/sh
  821.  
  822. #Purpose: To count number of vowels and consonants in a sentence.
  823.  
  824. # USAGE: sh vowelcons.sh (No arguments required)
  825.  
  826. echo “Enter a sentence:”
  827.  
  828. read line len=${#line} vowel=0
  829. x=0 i=1
  830. while test $i -le $len do
  831. k=`echo $line|cut -c $i | grep -ic [aeiou]` k1=`echo $line|cut -c $i|grep -ic [^a-z]` vowel=`expr $vowel + $k`
  832. x=`expr $x + $k1`
  833. 21
  834.  
  835.  
  836.  
  837.  
  838. i=`expr $i + 1`
  839.  
  840. done
  841.  
  842. echo “No of vowel:$vowel” con=`expr $len - $vowel - $x` echo “No of consonants:$con”
  843. #end of script
  844.  
  845.  
  846.  
  847.  
  848. 4) #! /bin/sh
  849.  
  850. #Purpose: To count number of users who are currently log ged in the system.
  851.  
  852. # USAGE: sh user.sh (NO arguments required)
  853.  
  854. echo “List of users connected:`who -q|head -1`”
  855.  
  856. echo “No of users:`who -q | tail -n -1 | cut -c 9`” #end of script
  857. 22
  858.  
  859.  
  860.  
  861.  
  862. Day 7
  863.  
  864. Lab Assignments:
  865.  
  866. 1. Write a shell script that displays a list of all files in the current directory to which you have read, write and execute permissions.
  867. 2. Write a shell script that lists files by modification time when called with lm and by access time when
  868. called with la. By default, the script should show the listing of all files in the current directory.
  869. 3. Write a shell script to display the files created or updated within fourteen days from the current date.
  870. 4. Develop a shell script which displays all files with all attributes those have been created or modified in the month of November.
  871.  
  872.  
  873. Solution: (LAB)
  874.  
  875. 1) #! /bin/sh
  876.  
  877. #Purpose: To display list of files in current directory to which owner have read,write and execute permission.
  878.  
  879. # USAGE: sh rwxperm.sh (no arguments required)
  880.  
  881. for var in *
  882.  
  883. do
  884.  
  885. if test -r $var -a -w $var -a -x $var -a ! -d $var then
  886. ls $var fi
  887. done
  888.  
  889. #end of script
  890.  
  891.  
  892.  
  893.  
  894. 2) #! /bin/sh
  895.  
  896. # Purpose: To list the files according to modification or access time depending on the arguments in command line.
  897.  
  898. # USAGE: sh filelist .sh lm(for modification time) or la (last access time)
  899.  
  900. case $1 in lm) ls -lt ;; la) ls -lut;;
  901. *) ls -l;;
  902. 23
  903.  
  904.  
  905.  
  906.  
  907. esac
  908.  
  909. #end of script
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916. 3) #! /bin/sh
  917.  
  918. # Purpose: To list the files created or updated within fourteen days from current date.
  919.  
  920. # USAGE: sh filecreat14.sh (no arguments required)
  921.  
  922. find -atime -14 -mtime -14 | sort -u
  923.  
  924. #end of script
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931. 4) #! /bin/sh
  932.  
  933. #Purpose: To list those files created or updated in month of November.
  934.  
  935. # USAGE: sh createNOV.sh (no arguments required)
  936.  
  937. for var in *
  938.  
  939. do
  940.  
  941. set -- `ls -l $var`
  942.  
  943. if test “$6” = “Nov”
  944.  
  945. then
  946.  
  947. ls -l $var fi
  948. done
  949.  
  950. #end of script
  951.  
  952.  
  953.  
  954.  
  955. Home assignments:
  956.  
  957. 1. Write a shell script to print last twenty commands issued by the user. The user name is supplied as a command line argument to the script (use bash-history file).
  958. 2. Write a shell program, which displays the message “welcome” and prints the date when you login to
  959. your system.
  960. 24
  961.  
  962.  
  963.  
  964.  
  965. 3. Accept a string from the terminal and echo a suitable message if it doesn‟t have at least ten
  966. characters.
  967. 4. Write a shell script which receives either the LOGNAME or the UID supplied at the command prompt and finds out at how many terminals this user has logged in.
  968. 5. Write a shell script, which gets executed the moment a user logs in. It should display the message “GOOD MORNING” or “GOOD AFTERNOON” or “GOOD EVENING” depending upon the time at which the user logs in.
  969.  
  970.  
  971.  
  972.  
  973.  
  974. Solution: (HOME)
  975.  
  976.  
  977.  
  978.  
  979. 1) #! /bin/sh
  980.  
  981. #Purpose: To print last 20 commands issued by the user.
  982.  
  983. # USAGE: sh 20comnd.sh username if test $# -ne 1
  984. then
  985.  
  986. echo “Enter your login name only”
  987.  
  988. exit fi
  989. if test “$1” = “$LOGNAME”
  990.  
  991. then
  992.  
  993. cat $HOME/.bash_history | tail -20 else
  994. echo “Wrong username”
  995.  
  996. fi
  997.  
  998. #end of script
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005. 2) #! /bin/sh
  1006.  
  1007. #Purpose: To print welcome and date when user log in to the system.
  1008.  
  1009. # USAGE: sh welcome.sh (no arguments required)
  1010. 25
  1011.  
  1012.  
  1013.  
  1014.  
  1015. # To be written in /.bashrc file
  1016.  
  1017. echo “Welcome”
  1018.  
  1019. echo `date`
  1020.  
  1021. #end of script
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028. 3) #! /bin/sh
  1029.  
  1030. #Purpose: To take a string and print a msg if the string have or not have atleast 10 characters.
  1031.  
  1032. # USAGE: sh string10char.sh string
  1033.  
  1034. If test $# -ne 1 then
  1035. echo “Enter a string.”
  1036.  
  1037. Exit fi
  1038. st=$1 v=${#st}
  1039. if test $v -lt 10 then
  1040. echo “String doesnot have atleast 10 characters.”
  1041.  
  1042. else
  1043.  
  1044. echo “String have atleast 10 characters”
  1045.  
  1046. fi
  1047.  
  1048. #end of script
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058. 4) #! /bin/sh
  1059. 26
  1060.  
  1061.  
  1062.  
  1063.  
  1064. #Purpose: To print number of terminals opened by the user.
  1065.  
  1066. # USAGE: sh terminal.sh loginname if test $# -ne 1
  1067. then
  1068.  
  1069. echo “Enter your login name only”
  1070.  
  1071. exit fi
  1072. if test “$1” = “$LOGNAME”
  1073.  
  1074. then
  1075.  
  1076. v=`ls /dev/pts | wc -w`
  1077.  
  1078. echo “Number of terminals=$v”
  1079.  
  1080. else
  1081.  
  1082. echo “Wrong login name”
  1083.  
  1084. fi
  1085.  
  1086. #end of script
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093. 5) #! /bin/sh
  1094.  
  1095. #Purpose:To print suitable msg depending on the time of the day when the user will log in.
  1096.  
  1097. # USAGE: sh message.sh (no arguments required)
  1098.  
  1099. # to be written in /.bashrc file var=`date +%H`
  1100. if test $var -lt 12 then
  1101. echo “Good morning”
  1102.  
  1103. elif test $var -lt 16 then
  1104. 27
  1105.  
  1106.  
  1107.  
  1108.  
  1109. echo “Good afternoon”
  1110.  
  1111. elif test $var -lt 20 then
  1112. echo “Good evening”
  1113.  
  1114. else
  1115.  
  1116. echo “Good night fi
  1117. #end of script
  1118. 28
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143. Day 8
  1144.  
  1145. Lab Assignments:
  1146. 1. Write a shell script, which reports names and sizes of all files in a directory (directory should be supplied as an argument to the shell script) whose size exceeds 100 bytes. The filenames should be printed in decreasing order of their sizes. The total number of such files should also be reported.
  1147. 2. Write a shell script that shows the names of all the non-directory files in the current directory and calculates the sum of the size of them.
  1148. 3. Write a shell script to list the name of files under the current directory that starts with a vowel.
  1149. 4. Write a shell script which receives two filenames as arguments and checks whether the two file‟s
  1150. contents are same or not. If they are same then the second file should be deleted.
  1151.  
  1152.  
  1153.  
  1154.  
  1155. Solution: (LAB)
  1156.  
  1157. 1) #! /bin/sh
  1158.  
  1159. #Purpose: To print the name of all files in a directory whose size exceeds 100 bytes along with total number of such files.
  1160.  
  1161. # USAGE: sh file100byte.sh directoryname
  1162.  
  1163. If test $# -ne 1 then
  1164.  
  1165.  
  1166.  
  1167. fi
  1168.  
  1169. cd $1
  1170.  
  1171. echo “Please give a directory name and try again”
  1172. exit
  1173.  
  1174. find -size +100b | sort –nr
  1175.  
  1176. echo “Total number of such files:”
  1177.  
  1178. find -size +100b | grep -c “.*”
  1179. 29
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185. #end of script
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191. 2) #! /bin/sh
  1192.  
  1193. #Purpose: To print all non-directory files in current directory alongwith the total size of those files.
  1194.  
  1195. #USAGE: sh sizenondir.sh (No arguments required)
  1196.  
  1197. sum=0
  1198.  
  1199. for var in *
  1200.  
  1201. do
  1202.  
  1203. if test ! -d $var
  1204.  
  1205. then
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212. fi done
  1213.  
  1214.  
  1215. ls $var
  1216.  
  1217. set -- `ls -l $var`
  1218.  
  1219. let sum=sum+$5
  1220.  
  1221. echo “Sum of sizes of all non directory files under current directory is:$sum”
  1222.  
  1223. #end of script
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229. 3) #! /bin/sh
  1230.  
  1231. #Purpose: To list the files starting with a vowel.
  1232.  
  1233. # USAGE : sh filevowel.sh (no arguments required)
  1234.  
  1235. echo “Required files are:”
  1236.  
  1237. ls | grep “^[aeiou]”
  1238.  
  1239. #end of script
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245. 4) #! /bin/sh
  1246. 30
  1247.  
  1248.  
  1249.  
  1250.  
  1251. #Purpose: To compare two files and delete the second file if they are equal.
  1252.  
  1253. # USAGE: sh filecomparedel.sh file1 file2 if test $# -ne 2
  1254. then
  1255.  
  1256. echo “Please give two filenames.”
  1257. exit fi
  1258.  
  1259. cmp -s $1 $2
  1260.  
  1261. if test $? -eq 0 then
  1262. echo “$1 and $2 are same”
  1263. rm $2
  1264.  
  1265. else
  1266.  
  1267. echo “$1 and $2 are not same”
  1268.  
  1269. fi
  1270.  
  1271. #end of script
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277.  
  1278. Home assignments:
  1279. 1. Write a shell script to check if a given file (filename supplied as command line argument) is a regular file or not and find the total number of words, characters and lines in it.
  1280. 2. Write a shell script which reads a directory name and compares the current directory with it (which has more files and how many more files).
  1281. 3. Write a shell script to check whether the given file is a blank file or not. If not found blank then display the contents of the file.
  1282. 4. Write a shell script to concatenate two files and count the number of characters, number of words and number of lines in the resultant file.
  1283. 5. Write a shell script that accepts two directory names, say mca1 and mca2 as arguments and deletes
  1284. those files in mca2 which have identical named files in mca1.
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291. Solution: (HOME)
  1292.  
  1293.  
  1294. 1) #! /bin/sh
  1295. 31
  1296.  
  1297.  
  1298.  
  1299.  
  1300. #Purpose: To check whether the given file is regular or not and find out total no. of words, total no. of characters and total no. of lines in it.
  1301.  
  1302. # USAGE: sh regularfile.sh filename if test $# -ne 1
  1303. then
  1304.  
  1305. echo “Please give a filename”
  1306. exit fi
  1307.  
  1308.  
  1309. if test -d $1
  1310.  
  1311. then
  1312.  
  1313. echo “It is a directory ,please give a file name”
  1314. exit fi
  1315.  
  1316. if test -f $1
  1317.  
  1318. . then
  1319.  
  1320. echo “It is a regular file”
  1321.  
  1322. echo “Total Words:`wc -w $1 | cut -d “ “ -f 1`”
  1323.  
  1324. echo “Total characters:`wc -c $1 | cut -d “ “ -f 1`”
  1325.  
  1326. echo “Total lines:`wc -l $1 | cut -d “ “ -f 1`”
  1327.  
  1328. else
  1329.  
  1330.  
  1331. echo “It is not a regular file”
  1332.  
  1333. fi
  1334.  
  1335. #end of script
  1336.  
  1337.  
  1338.  
  1339.  
  1340. 2) #! /bin/sh
  1341.  
  1342. #Purpose: To check two directories and compare how many files between them.
  1343.  
  1344. #USAGE: sh dircheck.sh (No arguments required)
  1345.  
  1346. echo -n “Enter a directory name:”
  1347.  
  1348. read dirc x=`pwd`
  1349. 32
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355. v1=`pwd | ls |wc -w`
  1356.  
  1357. echo “Files in `pwd` =$v1”
  1358.  
  1359. cd $dirc y=`pwd`
  1360. v2=`ls |wc -w`
  1361.  
  1362. echo “Files in `pwd`=$v2”
  1363. if test $v1 -eq $v2 then
  1364.  
  1365. echo “Both directory have same no of files”
  1366. fi
  1367.  
  1368. if test $v1 -gt $v2
  1369.  
  1370. then
  1371.  
  1372.  
  1373. echo “Current directory $x contains `expr $v1 - $v2` more files than given
  1374. directory $y”
  1375.  
  1376. else
  1377.  
  1378.  
  1379. echo “Given directory $y contains `expr $v2 - $v1` more files than current
  1380. directory $x”
  1381.  
  1382. fi
  1383.  
  1384. #end of script
  1385.  
  1386.  
  1387.  
  1388. 3) #! /bin/sh
  1389.  
  1390. #Purpose: To check whether the given file is blank or not.
  1391.  
  1392. # USAGE: sh blankfilecheck.sh filename if test $# -ne 1
  1393. then
  1394.  
  1395. echo “Enter a filename and try again”
  1396. exit fi
  1397.  
  1398. if test -d $1 then
  1399. echo “It is a directory,please enter a filename”
  1400. 33
  1401.  
  1402.  
  1403.  
  1404.  
  1405. exit fi
  1406.  
  1407. set -- `ls -l $1` if test $5 -eq 0 then
  1408. echo “$1 is a blank file”
  1409.  
  1410. else
  1411.  
  1412. echo “$1 is not a blank file”
  1413.  
  1414. echo “Contents of the file:”
  1415.  
  1416. cat $1 fi
  1417. #end of script
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423. 4) #! /bin/sh
  1424.  
  1425. #Purpose: To concatenate two files and find the no. of words,char and lines of resultant file.
  1426.  
  1427. # USAGE: sh concatfileinform.sh file1 file2 if test $# -ne 2
  1428. then
  1429.  
  1430. echo “Enter two file names only”
  1431. exit fi
  1432.  
  1433. echo -n “Enter the name of resultant file where these two files are to be concatenated:”
  1434. read rfile
  1435.  
  1436. cat $1 $2 > rfile
  1437.  
  1438. echo “Contents of resultant file:”
  1439.  
  1440. cat $rfile
  1441.  
  1442. echo “Resultant file have:”
  1443.  
  1444. echo “Number of words:`wc -w $rfile| cut -d “ “ -f 1`”
  1445. 34
  1446.  
  1447.  
  1448.  
  1449.  
  1450. echo “Number of characters:`wc -c $rfile| cut -d “ “ -f 1`”
  1451.  
  1452. echo “Number of lines:`wc -l $rfile| cut -d “ “ -f 1`”
  1453.  
  1454. #end of script
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460. 5) #! /bin/sh
  1461.  
  1462. #Purpose: To check two directories and delete files of second directory which is identical to the files of first directory.
  1463.  
  1464. #USAGE : sh identicalfiledel.sh directory1 directory2 if test $# -ne 2
  1465. then
  1466.  
  1467. echo “Enter two directory names only”
  1468.  
  1469. exit fi
  1470. if test -d $1 -a -d $2 then
  1471. echo “$1 and $2 are directories”
  1472.  
  1473. else
  1474.  
  1475. echo “$1 and $2 are not directories”
  1476.  
  1477. exit fi
  1478.  
  1479.  
  1480. echo “Files in directory $1=`ls $HOME/$1`” echo “Files in directory $2=`ls $HOME/$2`” f=0
  1481. cd $1
  1482.  
  1483. for var1 in *
  1484.  
  1485. do
  1486.  
  1487. cd $HOME/$2
  1488. 35
  1489.  
  1490.  
  1491.  
  1492.  
  1493. for var2 in *
  1494.  
  1495. do
  1496.  
  1497. cmp -s “$var1” “$var2”
  1498.  
  1499. if test $? -eq 0 then
  1500. f=1
  1501.  
  1502. echo “$var2 is present in $1 and $2 with identical names”
  1503.  
  1504. rm $var2
  1505.  
  1506. echo “$var2 is removed from $2”
  1507.  
  1508. fi done
  1509. done
  1510.  
  1511. if test $f -eq 0 then
  1512. echo “There are no identical named files in $1 and $2”
  1513.  
  1514. else
  1515.  
  1516. echo “Present files in $2 are:”
  1517.  
  1518. ls $HOME/$2 fi
  1519. #End of script
  1520. 36
  1521.  
  1522.  
  1523.  
  1524.  
  1525. Day 9
  1526.  
  1527. Lab Assignments:
  1528. 1. A file called list consists of several words. Write a shell script which will receive a list of filenames, the first of which would be list. The shell script should report all occurrences of each word in list in the rest of the files supplied as arguments.
  1529. 2. Write a shell script which deletes all lines containing the word UNIX in the files supplied as arguments to this shell script.
  1530. 3. Write a shell script which would receive a log name during execution, obtain information about it from password file and display this information on the screen in easily understandable format.
  1531. 4. Write a shell script, which will receive either the filename or the filename with its full path during execution. This script should print information about the file as given by ls –l command and display it in
  1532. an informative manner.
  1533.  
  1534.  
  1535.  
  1536.  
  1537.  
  1538. Solution: (LAB)
  1539.  
  1540. 1) #! /bin/sh
  1541.  
  1542. #Purpose: To consider a file and compare all occurrences of words of that file with the files supplied as arguments.
  1543.  
  1544. #USAGE: sh listoccurence.sh list file1 file2 file3………file n
  1545.  
  1546. If test $# -eq 0 then
  1547. echo “Please give filenames”
  1548.  
  1549. exit fi
  1550. l=$1 shift
  1551. for var1 in $*
  1552.  
  1553. do
  1554.  
  1555. for var2 in `cat $l`
  1556.  
  1557. do
  1558.  
  1559. echo “Occurrence of $var2 in $var1 is :`grep -c $var2 $var1`”
  1560.  
  1561. done done
  1562. 37
  1563.  
  1564.  
  1565.  
  1566.  
  1567. #end of script
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574. 2) #! /bin/sh
  1575.  
  1576. #Purpose: To delete the lines containing the word UNIX in the file supplied.
  1577.  
  1578. #USAGE: sh unixdelete.sh filename(s) If test $# -eq 0
  1579. then
  1580.  
  1581. echo “Please give filenames”
  1582.  
  1583. exit fi
  1584. for var in $*
  1585.  
  1586. do
  1587.  
  1588. grep -v “UNIX” $var >ff cp ff $var
  1589. done
  1590.  
  1591. #end of script
  1592.  
  1593.  
  1594.  
  1595.  
  1596.  
  1597.  
  1598. 3) #! /bin/sh
  1599.  
  1600. #Purpose: To print the user information in a informative manner.
  1601.  
  1602. #USAGE: sh userinform.sh loginname if test $# -ne 1
  1603. then
  1604.  
  1605. echo “Enter your login name only”
  1606.  
  1607. exit fi
  1608. grep -w “$1” “/etc/passwd”>file1
  1609. 38
  1610.  
  1611.  
  1612.  
  1613.  
  1614. IFS=:
  1615.  
  1616. If test $? -eq 0 then
  1617. set -- `grep -w “$1” “/etc/passwd”`
  1618.  
  1619. echo “Loginame:$1” echo “Password:$2” echo “UID:$3”
  1620. echo “GUID:$4”
  1621.  
  1622. echo “Home:$5”
  1623.  
  1624. echo “Login shell:$7”
  1625.  
  1626. else
  1627.  
  1628. echo “User doesnot exist”
  1629.  
  1630. fi
  1631.  
  1632. #end of script
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639. 4) #! /bin/sh
  1640.  
  1641. #Purpose: To display all the attributes of file in a informative manner.
  1642.  
  1643. # USAGE: sh fileinform.sh filename if test $# -ne 1
  1644. then
  1645.  
  1646. echo “Enter a file name”
  1647.  
  1648. exit fi
  1649. set -- `ls - l $1`
  1650.  
  1651. echo “File permission:$1”
  1652.  
  1653. echo “Links:$2”
  1654.  
  1655. echo “Username:$3”
  1656. 39
  1657.  
  1658.  
  1659.  
  1660.  
  1661. echo “Groupuser name:$4”
  1662.  
  1663. echo “Size:$5” echo “Month:$6” echo “Date:$7”
  1664. echo “Time:$8” #end of script
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679. Home assignments:
  1680. 1. Write a shell script that takes a list of names and displays all information in the password file, where login names are the members of the list.
  1681. 2. Write a shell script that periodically count the number of users logged into the system. Send the number of minutes at which to check as parameter.
  1682. 3. Write a shell script to count the number of words of different length present in a given text.
  1683. 4. Write a shell script to count the frequency of different words used in a file.
  1684. A shell script receives even number of filenames. Suppose four filenames are supplied then the first file should get copied into the second file, the third file should get copied into the fourth file, and so on. If odd number of filenames is supplied then no copying should take place and an error message should be displayed.
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691. solution: (HOME)
  1692.  
  1693.  
  1694.  
  1695.  
  1696. 1) #! /bin/sh
  1697.  
  1698. #Purpose: To take list of login names and print there attributes in informative manner from password file.
  1699.  
  1700. #USAGE: sh usersinform.sh loginname(s)
  1701.  
  1702. if test $# -eq 0 then
  1703. echo “Enter your login names please”
  1704.  
  1705. exit
  1706. 40
  1707.  
  1708.  
  1709.  
  1710.  
  1711. fi
  1712.  
  1713. for var in $*
  1714.  
  1715. do
  1716.  
  1717. grep -w “$var” “/etc/passwd” > file1
  1718.  
  1719. IFS=:
  1720.  
  1721. If test $? -eq 0 then
  1722. set -- `grep -w “$var” “/etc/passwd”`
  1723.  
  1724. echo “information of $var is:”
  1725.  
  1726. echo “Loginame:$1” echo “Password:$2” echo “UID:$3”
  1727. echo “GUID:$4”
  1728.  
  1729. echo “Home:$5”
  1730.  
  1731. echo “Login shell:$7”
  1732.  
  1733. echo else
  1734. echo “User doesnot exist”
  1735.  
  1736. fi done
  1737. #end of script
  1738.  
  1739.  
  1740.  
  1741.  
  1742. 2) #! /bin/sh
  1743.  
  1744. #Purpose: To generate a script which will print no. of users currently logged in according to the time supplied.
  1745.  
  1746. #USAGE: sh usersattime.sh time(in terms of second)
  1747.  
  1748. if test $# -ne 1 then
  1749. echo "please give time in minute in terms of seconds" exit
  1750. 41
  1751.  
  1752.  
  1753.  
  1754.  
  1755. fi
  1756.  
  1757. while true do
  1758. c=`who|grep -c ".*"`
  1759.  
  1760. echo "number of users=$c"
  1761.  
  1762.  
  1763. done
  1764.  
  1765. sleep $1
  1766.  
  1767. #end of script
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773. 3) #! /bin/sh
  1774.  
  1775. #Purpose: To print the occurrence of lenghth of different word in a text.
  1776.  
  1777. #USAGE: sh wordlengthoccur.sh (no arguments required)
  1778.  
  1779. echo "Enter a text:" read n
  1780. echo $n|tr -cd "[\040a-zA-Z]" > ff for var in `cat ff`
  1781. do
  1782.  
  1783. echo ${#var} >> f1 done
  1784. echo “Number of words of different lengths are:”
  1785.  
  1786. cat f1|sort|uniq –c rm f1
  1787. # end of script
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794. 4) #! /bin/sh
  1795.  
  1796. #Purpose: To print the frequency of different words in a file.
  1797.  
  1798. #USAGE: sh freqword.sh filename
  1799.  
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806. 42
  1807.  
  1808.  
  1809.  
  1810.  
  1811. if test $# -ne 1
  1812.  
  1813. then
  1814.  
  1815.  
  1816.  
  1817. fi
  1818.  
  1819.  
  1820. echo "please give file name" exit
  1821.  
  1822. echo “Frequency of different words in the file $1:”
  1823.  
  1824. tr ' ' '\n' < $1|tr -cd "[\na-zA-Z]" |sort| uniq –c
  1825.  
  1826. #end of script
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833. 5) #! /bin/sh
  1834.  
  1835. #Purpose: To concatenate the first file to second,third to fourth and so on,if the total number of arguments is even or else do nothing.
  1836.  
  1837. #USAGE: sh evnoddfiles.sh filename(s)
  1838.  
  1839. if test $# -eq 0 then
  1840. echo "give file names" exit
  1841. fi
  1842.  
  1843. v=`expr $# % 2`
  1844.  
  1845. k=$#
  1846.  
  1847. c=0
  1848.  
  1849. if test $v -eq 0 then
  1850.  
  1851.  
  1852.  
  1853. for var in $*
  1854. do
  1855.  
  1856. c=`expr $c + 2`
  1857.  
  1858. l=$1 m=$2
  1859. cp $l $m shift 2
  1860. if test $c -eq $k
  1861. 43
  1862.  
  1863.  
  1864.  
  1865.  
  1866.  
  1867.  
  1868.  
  1869.  
  1870. done
  1871.  
  1872. then fi
  1873.  
  1874.  
  1875. exit 1
  1876.  
  1877.  
  1878. else
  1879.  
  1880. fi
  1881.  
  1882.  
  1883. echo "No copy should take place as odd number of files are supplied"
  1884.  
  1885. #end of script
  1886. 44
  1887.  
  1888.  
  1889.  
  1890.  
  1891. Day 10
  1892.  
  1893.  
  1894.  
  1895.  
  1896. Lab Assignments:
  1897. 1. Devise a menu-driven shell program that accepts values from 1 to 4 and performs action depending upon the number keyed in:
  1898.  
  1899. 1. List of users currently logged in
  1900. 2. Present date
  1901. 3. Present working directory
  1902. 4. Quit
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908. solution: (LAB)
  1909.  
  1910. 1) #! /bin/sh
  1911.  
  1912. #USAGE: sh menu.sh (no arguments required)
  1913.  
  1914. while true do
  1915. echo “1. List of users currently logged in.”
  1916.  
  1917. echo “2. Present date”
  1918.  
  1919. echo “3. Present working directory”
  1920.  
  1921. echo “ 4. Quit”
  1922.  
  1923. echo -n “Enter your choice:”
  1924.  
  1925. read ch case $ch in
  1926. 1) who;;
  1927.  
  1928. 2) date +%d-%B-%Y;;
  1929.  
  1930. 3) pwd;;
  1931.  
  1932. 4) exit;;
  1933.  
  1934. *) echo “Invalid choice.”;;
  1935.  
  1936. esac
  1937.  
  1938. #end of script
  1939. 45
  1940.  
  1941.  
  1942.  
  1943.  
  1944. Home assignments:
  1945.  
  1946. 1. Write a shell script to make a password based menu-driven program, which will give a maximum of three chances to enter the password. If the given password is correct then the program will show the
  1947. 1. Number of users currently logged in.
  1948. 2. Calendar of current month.
  1949. 3. Date in the format: dd / mm / yyyy.
  1950. 4. Quit
  1951. The menu should be placed approximately in the centre of the screen and should be displayed in bold.
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958. solution: (HOME)
  1959.  
  1960.  
  1961.  
  1962.  
  1963. 1) #! /bin/sh
  1964.  
  1965. #USAGE: sh menupasswd.sh (no arguments required)
  1966.  
  1967. c=0 f=0
  1968. while test $f -eq 0 do
  1969. echo "Enter ur login name:" read n
  1970.  
  1971. if test $n != $LOGNAME
  1972. then
  1973. f=0
  1974.  
  1975. c=`expr $c + 1`
  1976.  
  1977. if test $c -eq 3 then
  1978.  
  1979.  
  1980.  
  1981. else fi
  1982. done
  1983.  
  1984.  
  1985.  
  1986.  
  1987. fi f=1
  1988.  
  1989. echo "3 chances over" exit 1
  1990.  
  1991. if test $f -eq 1
  1992.  
  1993. then
  1994.  
  1995.  
  1996. echo "Login name valid" while true
  1997. 46
  1998.  
  1999.  
  2000.  
  2001.  
  2002. do
  2003.  
  2004. echo "1.List of users currently logged in" echo "2.calendar of current month" echo "3.Date in format:dd/mm/yyyy"
  2005. echo "4.Exit"
  2006.  
  2007. echo "Enter ur choice:" read ch
  2008. case $ch in
  2009.  
  2010. 1) who -q|tail -1;;
  2011.  
  2012. 2) cal;;
  2013.  
  2014. 3) date +%d/%m/%Y;;
  2015.  
  2016. 4) exit;;
  2017.  
  2018. *) echo "invalid entry";;
  2019.  
  2020.  
  2021. done
  2022.  
  2023. esac
  2024.  
  2025. fi
  2026.  
  2027.  
  2028.  
  2029.  
  2030. #end of script
  2031. 47
  2032.  
  2033.  
  2034.  
  2035.  
  2036. Day 11
  2037.  
  2038. Lab Assignments:
  2039.  
  2040. 1. sed assignments
  2041.  
  2042. a) Delete last line of a file.
  2043. b) Delete from third to the last line of a file.
  2044. c) Append the line “LET US STOP” after the last line.
  2045. d) Print first five lines of a file.
  2046. e) Remove all lines containing the word „BCA‟ from a file.
  2047. f) Append the third line after the last line in a file.
  2048. g) Replace all occurrences of „two‟ with „three‟ in a file. h) Cut and paste the third line after the last line in a file. i) Double-space a file.
  2049. j) Insert a blank line above every line that matches "MCA".
  2050.  
  2051.  
  2052. 2. awk assignments
  2053.  
  2054. a) List all regular files in the current directory whose name starts with „a‟.
  2055. b) Display the current date and month in the given format:
  2056. c) Date: 22/11/2007 d) Month: Nov
  2057. e) List all files in the current directory whose size is greater than 200 bytes, and also give the sum of the
  2058. sizes of all those files.
  2059. f) List the name of files under the current directory that starts with a vowel. g) Check whether a given string is palindrome or not.
  2060. h) List all users currently logged into the system.
  2061. i) List all files in the current directory that has more than one link to it. j) Double-space a file.
  2062. k) Double-space a file that already has blank lines in it. Output file should contain no more than one
  2063. blank line between lines of text.
  2064. l) Number each line of file, but only print numbers if line is not blank.
  2065.  
  2066.  
  2067.  
  2068.  
  2069. Solutions: (LAB)
  2070.  
  2071. Sed assignments:
  2072.  
  2073. 1) a) $ sed „$d‟ file
  2074.  
  2075. b) $ sed „3,$d, file
  2076.  
  2077. c) $ sed „$a\LET US STOP\‟ file
  2078.  
  2079. d) $ sed „5q‟ file
  2080.  
  2081. e) $ sed -n „/BCA/!p‟ file
  2082.  
  2083. f) $ sed -n „p‟ file ; sed -n „3p‟ file
  2084. 48
  2085.  
  2086.  
  2087.  
  2088.  
  2089. g) $ sed „s/two/three/‟ file
  2090.  
  2091. h) $ sed -n „3!p‟ file ; sed -n „3p‟ file
  2092.  
  2093. i) $ sed „‟i\ \‟ file
  2094.  
  2095. j) $ sed „/MCA/i\ \‟ file
  2096.  
  2097.  
  2098.  
  2099. Awk assignments:
  2100.  
  2101. 2) a) $ ls | awk „/^a/{print}‟ file
  2102.  
  2103. b,c,d) $ date +”%d %m %Y %h”|awk -F “ “ „{print “Date:” $1”/”$2”/”$3 ; print
  2104. “Month:” $4}‟
  2105.  
  2106. e) $ ls -l | awk -F “ “ „BEGIN{printf “List of files:\n”} ; $5>200{s=s+$5;printf
  2107. “%s\n”,$9}; END{print “Sum of sizes:” s}‟
  2108.  
  2109. f) $ ls|awk „/^[aeiou]/{print}‟
  2110.  
  2111. g) #!/bin/sh
  2112. #Program: palin.sh
  2113.  
  2114. #Purpose: To prove whether a string is palindrome or not using awk command
  2115.  
  2116. #usage: sh palin.sh
  2117.  
  2118. echo –n ”Enter any string:”
  2119.  
  2120. read a junk
  2121.  
  2122. echo “$a” | awk „
  2123.  
  2124. {
  2125. p=$1
  2126. stat=”Palindrome” l=length(p) loop=int(l/2)
  2127. for(i=1;i<=loop;i++)
  2128. {
  2129. first=substr(p,i,1)
  2130. last=substr(p,l-i+1,1)
  2131.  
  2132. if(first != last)
  2133. stat=”Not palindrome”
  2134. }
  2135. print stat
  2136. }‟
  2137.  
  2138.  
  2139.  
  2140.  
  2141. h) $ who |awk -F “ “ „{print $1}‟
  2142. 49
  2143.  
  2144.  
  2145.  
  2146.  
  2147. i) $ ls -l |awk -F “ “ „$2>1{print $9}‟
  2148.  
  2149. j) $ awk „1; {print “”}‟ file
  2150.  
  2151. k) $ awk „NF{print $0 “\n”}‟ file
  2152.  
  2153. l) $ awk „NF{$0=++a “:” $0} ; {print}‟ file
  2154.  
  2155.  
  2156. Home assignments:
  2157.  
  2158. 1. sed assignments
  2159.  
  2160. a) Number each line of file, but only print numbers if line is not blank. b) Count lines (emulate "wc -l").
  2161. c) If a line ends with a backslash, append the next line to it.
  2162. d) Add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.).
  2163. e) Print the line immediately after the line containing “MCA”, but not the line containing “MCA”.
  2164.  
  2165. f) Print line number 5.
  2166. g) Beginning at line 3, print every 7th line.
  2167.  
  2168.  
  2169. 2. awk assignments
  2170.  
  2171. a) Count lines (emulate "wc -l").
  2172. b) Print the sums of the fields of every line.
  2173. c) Add all fields in all lines and print the sum.
  2174. d) Print every line after replacing each field with its absolute value. e) Print the total number of fields ("words") in all lines.
  2175. f) Print the total number of lines that contain "MCA".
  2176. g) Print the largest first field and the line that contains it (Intended for finding the longest string in field).
  2177.  
  2178. h) Print the number of fields in each line, followed by the line. i) Sort the login names of all users and print it.
  2179. j) Print only lines of less than 65 characters. k) Print line number 12.
  2180.  
  2181.  
  2182.  
  2183.  
  2184. Solution(HOME):-
  2185.  
  2186.  
  2187.  
  2188.  
  2189. Sed assignment:
  2190.  
  2191.  
  2192.  
  2193.  
  2194. 1)a) $ sed „/./=‟ file| sed „/./N ; s/\n/ /‟
  2195.  
  2196. b) $ sed -n „$=‟ file
  2197.  
  2198. c) $ sed -e :a -e „/\\$/N ; s/\\\n// ; ta‟ file
  2199. 50
  2200.  
  2201.  
  2202.  
  2203.  
  2204. d) $ sed „n;n;n;n;G;‟ file
  2205.  
  2206. e) $ sed -n „/MCA/{n;p;}‟ file
  2207.  
  2208. f) $ sed -n „5p‟ file
  2209.  
  2210. g) sed -n „3,${p;n;n;n;n;n;n;}‟ file
  2211.  
  2212.  
  2213.  
  2214.  
  2215. awk assignment:
  2216.  
  2217. 2)a) $ awk „END{print NR}‟ file
  2218.  
  2219. b) $ awk „{s=0; for(i=1 ; i<=NF ; i=i+1) s=s+i ; print s}‟ file
  2220.  
  2221. c) $ awk „{for(i=1 ; i<=NF ; i=i+1) s= s+i ; END{print s}‟ file
  2222.  
  2223. d) $ awk „{for(i=1 ; i<=NF ; i=i+1) print “ “ i ; printf “\n%s\n”, $0}‟ file
  2224.  
  2225. e) $ awk „{total=total+NF} ; END{print total}‟ file
  2226.  
  2227. f) $ awk „/MCA/{n++} ; END {print n}‟ file
  2228.  
  2229. g) $ awk „$1>max1{max=$1 ; max2=$0} ; END{print max1,max2}‟ file
  2230.  
  2231. h) $ awk „{print NF “:” $0}‟ file
  2232.  
  2233. i) $ awk -F “:” „{print $1 | “sort”}‟ /etc/passwd
  2234.  
  2235. j) $ awk „length<65‟ file
  2236.  
  2237. k) $ awk „NR==12‟ file
  2238. 51
  2239.  
  2240.  
  2241.  
  2242.  
  2243. Day 12
  2244.  
  2245. Lab Assignments:
  2246.  
  2247.  
  2248.  
  2249. 1. A text file contains information in the following format in each line of the text: Date Product Code Quantity Indicator (In/Out)
  2250.  
  2251. ------ ----------------- ----------- ---------------------
  2252.  
  2253. | | | |
  2254.  
  2255. | | | |
  2256.  
  2257. | | | |
  2258.  
  2259. Write a shell script to calculate the cumulative stock of any particular product (either input from keyboard or command line argument) at any date.
  2260.  
  2261.  
  2262.  
  2263. 2. A file contains marks of examination in the following format:
  2264. Name Subject1 Subject2 Subject3 Subject4 Subject5
  2265.  
  2266. ------- ---------- ---------- ----------- ----------- -----------
  2267.  
  2268. . . . . . .
  2269.  
  2270. . . . . . .
  2271.  
  2272. . . . . . .
  2273.  
  2274. Write a shell script to arrange the records according to the total marks of all subjects in the descending order.
  2275.  
  2276.  
  2277.  
  2278.  
  2279.  
  2280.  
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286.  
  2287.  
  2288.  
  2289.  
  2290.  
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296. Solution : (LAB)
  2297.  
  2298. 1) stock database:(stocklist.txt)
  2299. 52
  2300.  
  2301.  
  2302.  
  2303. Date Productcode Quantity indicator(in/out)
  2304. 07/11/09 001 25 In
  2305. 07/11/09 002 30 Out
  2306. 07/11/09 001 50 In
  2307. 11/11/09 003 60 In
  2308. 12/11/09 002 70 Out
  2309. 12/11/09 002 75 In
  2310. 11/11/09 003 80 Out
  2311.  
  2312.  
  2313. Shell script:
  2314.  
  2315. #!/bin/sh
  2316.  
  2317. #Program: stock.sh
  2318.  
  2319. #Purpose: To calculate the cumilitive stock of any particular product at any date.
  2320.  
  2321. #Usage: sh stock.sh stocklist.txt if test $# -ne 1
  2322. then
  2323.  
  2324. echo “Enter the database file as input in command line.”
  2325.  
  2326. exit fi
  2327. awk 'BEGIN { printf "\nEnter date:" getline c <"/dev/tty"
  2328. printf "\nEnter code:"
  2329.  
  2330. getline c1 <"/dev/tty"
  2331.  
  2332. printf "Totalstock\n"
  2333. }
  2334.  
  2335. $1==c && $2==c1{x=x+$3} END{printf "%d\n",x} ' $1
  2336. #end of script
  2337.  
  2338.  
  2339.  
  2340.  
  2341. 2) student database: (studentinfo.txt)
  2342.  
  2343. Name Sub1 Sub2 sub3 sub4 sub5
  2344. ajay 56 58 69 70 75
  2345. bimal 66 67 70 85 98
  2346. ajoy 87 88 89 90 92
  2347.  
  2348. 53
  2349.  
  2350.  
  2351.  
  2352. raj 55 54 45 50 57
  2353. komol 78 77 71 82 83
  2354. sam 91 92 93 55 50
  2355.  
  2356.  
  2357. shell script:
  2358.  
  2359. #! /bin/sh
  2360.  
  2361.  
  2362. #Program: student.sh
  2363.  
  2364. #Purpose: To arrange the records according to the total marks of all subjects in descending order.
  2365.  
  2366. #Usage: sh student.sh studentinfo.txt if test $# -ne 1
  2367. then
  2368.  
  2369. echo “Enter the database file as input in command line.”
  2370.  
  2371. exit fi
  2372. awk ' BEGIN {printf "NAME Total marks\n"} NR>1{
  2373. x=0
  2374.  
  2375. for(i=2;i<=NF;i++)
  2376.  
  2377. x=x+$i
  2378.  
  2379. printf "\n%s\t%d",$1,x|"sort -nr -k 2"
  2380.  
  2381. } ' $1
  2382.  
  2383. #end of script
  2384.  
  2385.  
  2386.  
  2387.  
  2388.  
  2389. Home Assignments:
  2390.  
  2391. 1. Write a shell function size( ) which lists only the total size of the files(filenames supplied as arguments).
  2392. 2. Write a shell script for renaming each file in the directory such that it will have the current shell
  2393. PID as an extension. The shell script should ensure that the directories do not get renamed.
  2394. 3. Write a shell script to sort a list of elements using bubble sort technique.
Add Comment
Please, Sign In to add comment