Simple shell ("sh") database.

From the Rosetta Code



---------------------------------

$ sdb create CDs
Create DB `CDs'
$ sdb add CDs Bookends
$ sdb prop CDs Bookends artists "Simon & Garfunkel"
$ sdb add CDs "Ode to joy"
$ sdb prop CDs "Ode to joy" artist "Beethoven"
$ sdb tag CDs Bookends rock folk  # I'm not sure about this
$ sdb tag CDs "Ode to joy" classical
$ sdb show CDs Bookends
Description:
 
artists:
    Simon & Garfunkel
 
Tags: folk  rock
$ sdb prop CDs "Ode to joy" Description "Sym. No. 9"
$ sdb show CDs "Ode to joy"
Description:
    Sym. No. 9
 
artist:
    Beethoven
 
Tags: classical
$ sdb list-all CDs
Tag: classical
    Ode to joy
 
Tag: folk
    Bookends
 
Tag: rock
    Bookends
 
$ sdb drop CDs
Delete DB `CDs'
$
 
--------------------
 #!/bin/sh
 
db_create() {
 mkdir ./"$1" && mkdir "./$1/.tag" && echo "Create DB \`$1'"
}
 
db_delete() {
 rm -r ./"$1" && echo "Delete DB \`$1'"
}
 
db_show() {
 if [ -z "$2" ]; then show_help; fi
 for x in "./$1/$2/"*; do
  echo "$x:" | sed "s/.*\///"
  cat "$x" | sed "s/^/    /"
  echo
 done
 
 printf "Tags: "
 ls "./$1/$2/.tag"
}
 
db_tag() {
 local db="$1" item="$2"
 shift
 shift
 for tag in $@; do
  mkdir "./$db/.tag/$tag"
  ln -s "$PWD/$db/$item" "./$db/.tag/$tag/"
  touch "./$db/$item/.tag/$tag"
 done
}
 
show_help() {
 echo "Usage: $0 command [args]"
 echo "Commands:"
 cat $0 | grep ") ##" | grep -v grep | sed 's/) ## /:\t/'
 exit
}
 
if [ -z "$1" ]; then show_help; fi
 
action=$1 it=database
shift
case $action in
 create) ## db -- create $it
  db_create "$@" ;;
 
 drop) ## db -- delete $it
  db_delete "$@" ;;
 
 add) ## db item -- add new item to $it
  mkdir -p "./$1/$2/.tag" && touch "./$1/$2/Description" ;;
 
 rem) ## db item -- delete item from $it
  rm -r "./$1/$2"
  rm "./$1/.tag/"*"/$2"
  ;;
 
 show) ## db item -- show item
  db_show "$@" ;;
 
 newtag) ## db new-tag-name -- create new tag name
  mkdir "./$1/.tag/$2" ;;
 
 prop) ## db item property-name property-content -- add property to item
  echo "$4" > "./$1/$2/$3" ;;
 
 tag) ## db item tag [more-tags...] -- mark item with tags
  db_tag "$@" ;;
 
 last) ## db -- show latest item
  ls "$1" --sort=time | tail -n 1
  ;;
 
 list) ## db -- list all items
  ls "$1" -1 --sort=time
  ;;
 
 list-all) ## db -- list items in each category
  for x in "$1/.tag/"*; do
   echo "$x" | sed 's/.*\//Tag: /'
   printf "    "
   ls "$x" --sort=time | tail -n 1
   echo
  done
  ;;
 
 help) ## this message
  show_help
  ;;
 
 *) echo Bad DB command: $1
  show_help
  ;;
esac

Comments

Popular posts from this blog

Guiless?

MSOffice vs Libreoffice

Raspberry pi zero.