• Source
    1. #
    2. #Write a Script for Simple Database Management System Operation.
    3. #Database File Contains Following Fields.
    4. #EMP_NO
    5. #EMP_NAME
    6. #EMP_ADDRESS
    7. #EMP_AGE
    8. #EMP_GENDER
    9. #EMP_DESIGNATION
    10. #EMP_BASIC_SALARY
    11. #
    12. #Provide Menu Driven Facility For
    13. #
    14. #VIEW RECORD BASED ON QUERY
    15. #ADD RECORD
    16. #DELETE RECORD
    17. #MODIFY RECORD.
    18. #COUNT TOTAL NUMBER OF RECORDS
    19. #EXIT
    20. #
    21.  
    22. clear
    23. ################################ FUNCTION DECLARATIONS ############################
    24. getTotalRecords()
    25. {
    26. total=`cat $dbfile | wc -l`
    27. echo "Total records :"$total
    28. }
    29. viewAllRecords()
    30. {
    31. total=`cat $dbfile | wc -l`
    32. echo "Total records :"$total
    33. if [ $total -gt 0 ]
    34. then
    35. i=1
    36. while [ $i -le $total ]
    37. do
    38. echo "===================================="
    39. echo "Employee - "$i
    40. echo "============="
    41. record=`cat $dbfile | head -$i | tail -1`
    42. emp_no=`echo $record | cut -d "|" -f1`
    43. emp_name=`echo $record | cut -d "|" -f2`
    44. emp_add=`echo $record | cut -d "|" -f3`
    45. emp_age=`echo $record | cut -d "|" -f4`
    46. emp_gen=`echo $record | cut -d "|" -f5`
    47. emp_desg=`echo $record | cut -d "|" -f6`
    48. emp_sal=`echo $record | cut -d "|" -f7`
    49. echo "Employee No. :" $emp_no
    50. echo "Employee Name :" $emp_name
    51. echo "Employee Address :" $emp_add
    52. echo "Employee Age :" $emp_age
    53. echo "Employee Gender :" $emp_gen
    54. echo "Employee Designation :" $emp_desg
    55. echo "Employee salary :" $emp_sal
    56. i=`expr $i + 1`
    57. echo "===================================="
    58. done
    59.  
    60. fi
    61. }
    62. addRecord()
    63. {
    64. echo "===================================="
    65. echo " ENTER EMPLOYEE INFORMATION"
    66. echo "===================================="
    67. echo -n "Enter employee no. :"
    68. read no
    69. echo -n "Enter employee name :"
    70. read name
    71. echo -n "Enter employee address :"
    72. read add
    73. echo -n "Enter employee age :"
    74. read age
    75. echo -n "Enter employee gender :"
    76. read gen
    77. echo -n "Enter employee designation :"
    78. read desc
    79. echo -n "Enter employee salary :"
    80. read sal
    81. record="$no|$name|$add|$age|$gen|$desc|$sal"
    82. echo $record >> $dbfile
    83. echo "Employee information added successfully."
    84. }
    85. getEmployeeInformation()
    86. {
    87. echo "===================================="
    88. echo " FIND EMPLOYEE INFORMATION"
    89. echo "===================================="
    90. echo -n "Enter employee no. :"
    91. read no
    92. total=`cat $dbfile | grep -cwi $no`
    93. if [ $total -gt 0 ]
    94. then
    95. record=`cat $dbfile | grep -wi $no`
    96. emp_no=`echo $record | cut -d "|" -f1`
    97. emp_name=`echo $record | cut -d "|" -f2`
    98. emp_add=`echo $record | cut -d "|" -f3`
    99. emp_age=`echo $record | cut -d "|" -f4`
    100. emp_gen=`echo $record | cut -d "|" -f5`
    101. emp_desg=`echo $record | cut -d "|" -f6`
    102. emp_sal=`echo $record | cut -d "|" -f7`
    103. echo "Employee No. :" $emp_no
    104. echo "Employee Name :" $emp_name
    105. echo "Employee Address :" $emp_add
    106. echo "Employee Age :" $emp_age
    107. echo "Employee Gender :" $emp_gen
    108. echo "Employee Designation :" $emp_desc
    109. echo "Employee salary :" $emp_sal
    110. elif [ $total -eq 0 ]
    111. then
    112. echo "No record found for this id"
    113. fi
    114. }
    115. deleteEmployee()
    116. {
    117. echo "===================================="
    118. echo " DELETE EMPLOYEE INFORMATION"
    119. echo "===================================="
    120. echo -n "Enter employee no. :"
    121. read no
    122. total=`cat $dbfile | grep -cwi $no`
    123. if [ $total -gt 0 ]
    124. then
    125. totalrecords=`cat $dbfile | wc -l`
    126. i=1
    127. while [ $i -le $totalrecords ]
    128. do
    129. record=`cat $dbfile | head -$i | tail -1`
    130. findrecord=`echo $record | grep -wci $no`
    131. if [ $findrecord -eq 0 ]
    132. then
    133. echo "$record" >> "tempdata"
    134. fi
    135. i=`expr $i + 1`
    136. done
    137. mv "tempdata" "data"
    138. echo "Employee with id $no deleted."
    139. elif [ $total -eq 0 ]
    140. then
    141. echo "No record found for this id"
    142. fi
    143. }
    144. modifyRecord()
    145. {
    146. echo "===================================="
    147. echo " MODIFY EMPLOYEE INFORMATION"
    148. echo "===================================="
    149. echo -n "Enter employee no. :"
    150. read no
    151. total=`cat $dbfile | grep -cwi $no`
    152. if [ $total -gt 0 ]
    153. then
    154. record=`cat $dbfile | grep -wi $no`
    155. echo "================================================="
    156. echo " ENTER EMPLOYEE INFORMATION FOR ID : $no"
    157. echo "================================================="
    158. echo -n "Enter employee name :"
    159. read name
    160. echo -n "Enter employee address :"
    161. read add
    162. echo -n "Enter employee age :"
    163. read age
    164. echo -n "Enter employee gender :"
    165. read gen
    166. echo -n "Enter employee designation :"
    167. read desc
    168. echo -n "Enter employee salary :"
    169. read sal
    170. updatedrecord="$no|$name|$add|$age|$gen|$desc|$sal"
    171. #updates=$(`cat $dbfile` | sed s/$record/$updatedrecord/g)
    172. totalrecords=`cat $dbfile | wc -l`
    173. i=1
    174. while [ $i -le $totalrecords ]
    175. do
    176. record=`cat $dbfile | head -$i | tail -1`
    177. findrecord=`echo $record | grep -wci $no`
    178. if [ $findrecord -eq 0 ]
    179. then
    180. echo "$record" >> "tempdata"
    181. else
    182. echo "$updatedrecord" >> "tempdata"
    183. fi
    184. i=`expr $i + 1`
    185. done
    186. mv "tempdata" "data"
    187. echo "Record updated successfully"
    188. else
    189. echo "Record with this id not found"
    190. fi
    191. }
    192. ############################ END OF FUNCTION DECLARATIONS #########################
    193. dbfile="data"
    194.  
    195. echo "===== AVAILABLE CHOICES ====="
    196. echo "1 - All records display"
    197. echo "2 - Add record"
    198. echo "3 - View record"
    199. echo "4 - Delete record"
    200. echo "5 - Modify record"
    201. echo "6 - Count total records"
    202. echo "E - Exit"
    203. echo "=============================="
    204. echo -n "Enter your choice :"
    205. read choice
    206. case $choice in
    207. "1")
    208. viewAllRecords
    209. ;;
    210. "2")
    211. addRecord
    212. ;;
    213. "3")
    214. getEmployeeInformation
    215. ;;
    216. "4")
    217. deleteEmployee
    218. ;;
    219. "5")
    220. modifyRecord
    221. ;;
    222. "6")
    223. getTotalRecords
    224. ;;
    225. [eE])
    226. exit
    227. ;;
    228. *)
    229. echo "Invalid choice"
    230. ;;
    231. esac