Light sql like database written in Bash


/ Published in: Bash
Save to your folder(s)

This was a school project, so it is not well commented but feel free to ask details.


Copy this code and paste it in your HTML
  1. ## This program is free software: you can redistribute it and/or modify
  2. ## it under the terms of the GNU General Public License as published by
  3. ## the Free Software Foundation, either version 3 of the License, or
  4. ## (at your option) any later version.
  5. ##
  6. ## This program is distributed in the hope that it will be useful,
  7. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. ## GNU General Public License for more details.
  10. ##
  11. ## You should have received a copy of the GNU General Public License
  12. ## along with this program. If not, see <http://www.gnu.org/licenses/>.
  13.  
  14.  
  15. #!/bin/sh
  16. ## bdsh.sh for bdsh in /u/all/benzia_a/cu/temp
  17. ## Made by chakib1 benziane
  18. ## Login <benzia_a@epitech.net>
  19. ##
  20. ## Started on Fri Jan 22 22:11:26 2010 chakib1 benziane
  21. ## Last update Fri Jan 22 22:57:44 2010 chakib1 benziane
  22. ##
  23.  
  24. ### BDSH V1.0
  25. ### Database Emulator with Shell
  26.  
  27. ### VARIABLES ####
  28.  
  29. TRUE=0
  30. FALSE=1
  31. E_NOARGS=68
  32. VERBOSE=1
  33. DB_FILE="sh.db"
  34. DATABASE_HEADER="BDSH DATABASE V1.0\nLast modification: `date`\n"
  35. SEPARATOR=' @__:::__@ '
  36. KEY=""
  37. VALUE=""
  38.  
  39.  
  40. #################################################
  41. #### FUNCTIONS ##########################
  42. #################################################
  43.  
  44.  
  45. usage ()
  46. {
  47. echo "Syntax error : Usage" >&2
  48. cat<<EOF
  49. bdsh.sh [-k] [(-f | -c) <db_file>] (put (<clef> | $<clef>) (<valeur> | $<clef>) |
  50.   del (<clef> | $<clef>) [<valeur> | $<clef>] |
  51.   select [<expr> | $<clef>]
  52. flush)
  53.  
  54. EOF
  55. }
  56.  
  57.  
  58. file_error ()
  59. {
  60. echo "No base found : $DB_FILE" >&2
  61. exit 1
  62. }
  63.  
  64. set_output ()
  65. {
  66. if [ ! -f "$DB_FILE" ]
  67. then
  68. echo "$DATABASE_HEADER" > "$DB_FILE"
  69. fi
  70. }
  71.  
  72.  
  73. db_put ()
  74. {
  75. if [ -n "$1" ] && [ -n "$2" ]
  76. then
  77. KEY="$1"
  78. VALUE="$2"
  79. set_output
  80. if [ `echo "$VALUE" | grep '^\$.*$'` ]
  81. then
  82. VALUE=`echo "$VALUE" | sed 's/^\$\([^ ]*\)/\1/g'`
  83. SEARCH_VALUE=`grep "^$VALUE $SEPARATOR" "$DB_FILE"`
  84. if [ -z "$SEARCH_VALUE" ]
  85. then
  86. echo "No such key : \$<$VALUE>" >&2
  87. exit 1
  88. else
  89. VALUE=`echo "$SEARCH_VALUE" | sed 's/.* \([^ ]*\)$/\1/g'`
  90. fi
  91. fi
  92. #### if in form $<key> ###
  93. if [ `echo "$KEY" | grep '^\$.*$'` ]
  94. then
  95. ### parse value of $key ###
  96. KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
  97. SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
  98. if [ -z "$SEARCH_KEY" ]
  99. then ### KEY NOT FOUND ###
  100. echo "No such key : \$<$KEY>" >&2
  101. exit 1
  102. else
  103. ### KEY FOUND ###
  104. KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
  105. SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
  106. if [ -z "$SEARCH_KEY" ]
  107. then
  108. echo "$KEY $SEPARATOR $VALUE" >> "$DB_FILE"
  109. else
  110. cat "$DB_FILE" | sed "/^$KEY $SEPARATOR/ s/[^ ]*$/$VALUE/g" > "$DB_FILE.temp"
  111. cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
  112. fi
  113. fi
  114. exit 0
  115. fi
  116. ### KEY in form <KEY>
  117. SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
  118. if [ -n "$SEARCH_KEY" ] ### if key exists
  119. then ### Replacing key
  120. cat "$DB_FILE" | sed "/^$KEY / s/[^ ]*$/$VALUE/g" > "$DB_FILE.temp"
  121. cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
  122. else ### Creating new key
  123. echo "$KEY $SEPARATOR $VALUE" >> "$DB_FILE"
  124. fi
  125. fi
  126. }
  127.  
  128. db_del ()
  129. {
  130. KEY="$1"
  131. VALUE="$2"
  132. ### if in form $key
  133. if [ `echo "$KEY" | grep '^\$.*$'` ]
  134. then
  135. ### parse value of $key ###
  136. KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
  137. SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
  138. KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
  139. fi
  140. if [ -z "$2" ]
  141. then
  142. ### searching key
  143. SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
  144. ### KEY FOUND ###
  145. if [ -n "$SEARCH_KEY" ]
  146. then
  147. VALUE=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
  148. cat "$DB_FILE" | sed "/^$KEY / s/[^ ]*$//g" > "$DB_FILE.temp"
  149. cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
  150. fi
  151. else
  152. ### Detecting value form
  153. if [ `echo "$VALUE" | grep '^\$.*$'` ]
  154. then
  155. VALUE=`echo "$VALUE" | sed 's/^\$\([^ ]*\)/\1/g'`
  156. SEARCH_VALUE=`grep "^$VALUE $SEPARATOR" "$DB_FILE"`
  157. VALUE=`echo "$SEARCH_VALUE" | sed 's/.* \([^ ]*\)$/\1/g'`
  158. fi
  159. if [ -n "$VALUE" ]
  160. then
  161. cat "$DB_FILE" | sed "/^$KEY/d" > "$DB_FILE.temp"
  162. cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
  163. fi
  164. fi
  165. exit 0
  166. }
  167.  
  168. db_select ()
  169. {
  170. if [ ! $1 ]
  171. then
  172. cat "$DB_FILE" | sed "s/^.* $SEPARATOR //g"
  173. exit 0
  174. else
  175. KEY="$1"
  176. ### case of $key
  177. if [ `echo "$KEY" | grep '^\$.*$'` ]
  178. then
  179. ### parse value of $key ###
  180. KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
  181. SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
  182. KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
  183. SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
  184. if [ -z "$SEARCH_KEY" ]
  185. then
  186. echo "No such key : \$<$KEY>" >&2
  187. exit 1
  188. fi
  189. VALUE=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
  190. if [ $VERBOSE = "0" ]
  191. then
  192. echo "<$KEY>=<$VALUE>"
  193. else
  194. echo "$VALUE"
  195. fi
  196. exit 0
  197. else
  198. if [ $VERBOSE = "0" ]
  199. then
  200. TMP=`grep ".*$KEY.* $SEPARATOR" "$DB_FILE"`
  201. if [ -z "$TMP" ]
  202. then
  203. echo "$KEY="
  204. else
  205. grep ".*$KEY.* $SEPARATOR" "$DB_FILE" | sed "s/ $SEPARATOR /=/g"
  206. fi
  207. exit 0
  208. else
  209. grep ".*$KEY.* $SEPARATOR" "$DB_FILE" | sed "s/^.* $SEPARATOR //g"
  210. fi
  211. exit 0
  212. fi
  213. fi
  214. }
  215.  
  216.  
  217. ##################################################
  218. #### PROGRAM STARTS HERE #########################
  219. ##################################################
  220.  
  221. if [ ! "$1" ]
  222. then
  223. echo "Syntax error : put" >&2
  224. exit $E_NOARGS
  225. fi
  226.  
  227. while [ $# -gt 0 ]; do
  228. case "$1" in
  229. -k)
  230. VERBOSE=0
  231. ;;
  232. -c|-f)
  233. DB_FILE="$2"
  234. if [ -n "$2" ]
  235. then
  236. if [ ! -f "$DB_FILE" ] && [ "$1" = "-f" ]
  237. then
  238. echo "No base found : $DB_FILE " >&2
  239. exit 1
  240. else
  241. shift
  242. fi
  243. else
  244. usage
  245. exit $E_NOARGS
  246. fi
  247. ;;
  248. -*)
  249. usage
  250. exit $E_NOARGS
  251. ;;
  252. put)
  253. if [ $# -ne 3 ]
  254. then
  255. echo "Syntax error : put" >&2
  256. exit 1
  257. else
  258. db_put "$2" "$3"
  259. shift;shift
  260. fi
  261. ;;
  262. del)
  263. if [ -n "$2" ] && [ ! $4 ]
  264. then
  265. db_del "$2" "$3"
  266. shift;shift
  267. else
  268. echo "Syntax error : put" >&2
  269. exit $E_NOARGS
  270. fi
  271. ;;
  272. select)
  273. if [ $# -lt 3 ]
  274. then
  275. db_select "$2"
  276. shift
  277. exit 0
  278. else
  279. echo "Syntax error : select" >&2
  280. exit $E_NOARGS
  281. fi
  282. ;;
  283. flush)
  284. if [ "$#" -eq "1" ]
  285. then
  286. echo "$DATABASE_HEADER" > "$DB_FILE"
  287. exit 0
  288. else
  289. usage
  290. exit $E_NOARGS
  291. fi
  292. ;;
  293. *)
  294. usage
  295. break;;
  296. esac
  297. shift
  298. done
  299.  
  300. #END

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.