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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.