Sha256: 24bd1736eebdbfe1755142a63643f762cfd9ce3a2f2e695c63009aae7001fb0a

Contents?: true

Size: 1.36 KB

Versions: 23

Compression:

Stored size: 1.36 KB

Contents

#!/usr/bin/env bash

usage()
{
  printf "

  Usage:

    db database_file {{key}} {{value}} # set
    db database_file {{key}}           # get
    db database_file {{key}} unset     # unset

" >&2
}

if [[ -f "$1" ]] ; then

  database_file="$1" ; shift

  if [[ ! -f "$database_file" ]] ; then

    directory=$(dirname "$database_file")

    if [[ ! -d "$directory" ]] ; then
      mkdir -p "$directory"
    fi

    touch "$database_file"

  fi

else

  printf "\n\nDatabase file $1 does not exist.\n\n" >&2

  exit 1

fi

key="$1" ; shift

if [[ -z "$key" ]] ; then

  usage

  exit 1

else

  value="$*"

  if [[ "unset" = "$value" || "delete" = "$value" ]] ; then

    sed -e "s#^$key=.*\$##" -e '/^$/d' "$database_file" > "$database_file.new"

    mv "$database_file.new" "$database_file"

  else

    if [[ -z "$value" ]] ; then # get

      awk -F= '/^'"$key"'=/' "$database_file" | sed -e "s#^$key=##" -e '/^$/d'

    else # set

      sed -e "s#^$key=.*\$##" -e '/^$/d' "$database_file" > "$database_file.new"

      mv "$database_file.new" "$database_file"

      if [[ -z "$(awk -F= "/^'"$key"'=/{print $2}" "$database_file")" ]] ; then # append

        echo "$key=$value" >> "$database_file"

      else # overwrite

        sed -i.tmp "s#^$key=.*\$#$key=$value#" "$database_file" > "$database_file.new"

        mv "$database_file.new" "$database_file"

      fi

    fi

  fi

fi

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
rvm-1.1.4 scripts/db
rvm-1.1.3 scripts/db
rvm-1.1.2 scripts/db
rvm-1.1.1 scripts/db
rvm-1.1.0 scripts/db
rvm-1.0.23 scripts/db
rvm-1.0.22 scripts/db
rvm-1.0.21 scripts/db
rvm-1.0.20 scripts/db
rvm-1.0.19 scripts/db
rvm-1.0.18 scripts/db
rvm-1.0.17 scripts/db
rvm-1.0.15 scripts/db
gemrage-1.0.0 vendor/ruby/1.8/gems/rvm-1.0.14/scripts/db
gemrage-0.4.1 vendor/ruby/1.8/gems/rvm-1.0.14/scripts/db
gemrage-0.4.0 vendor/ruby/1.8/gems/rvm-1.0.14/scripts/db
gemrage-0.3.2 vendor/ruby/1.8/gems/rvm-1.0.14/scripts/db
gemrage-0.3.1 vendor/ruby/1.8/gems/rvm-1.0.14/scripts/db
gemrage-0.3.0 vendor/ruby/1.8/gems/rvm-1.0.14/scripts/db
gemrage-0.2.0 vendor/ruby/1.8/gems/rvm-1.0.14/scripts/db