Return to Snippet

Revision: 42886
at March 12, 2011 06:27 by sp4ke


Updated Code
##    This program is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program.  If not, see <http://www.gnu.org/licenses/>.


#!/bin/sh
## bdsh.sh for bdsh in /u/all/benzia_a/cu/temp
## Made by chakib1 benziane
## Login   <[email protected]>
##
## Started on  Fri Jan 22 22:11:26 2010 chakib1 benziane
## Last update Fri Jan 22 22:57:44 2010 chakib1 benziane
##

### BDSH V1.0
### Database Emulator with Shell

### VARIABLES ####

TRUE=0
FALSE=1
E_NOARGS=68
VERBOSE=1
DB_FILE="sh.db"
DATABASE_HEADER="BDSH DATABASE V1.0\nLast modification:   `date`\n"
SEPARATOR='			@__:::__@			'
KEY=""
VALUE=""


#################################################
####       FUNCTIONS   ##########################
#################################################


usage ()
{
    echo "Syntax error : Usage" >&2
    cat<<EOF
bdsh.sh [-k] [(-f | -c) <db_file>] (put (<clef> | $<clef>) (<valeur> | $<clef>) |
                                    del (<clef> | $<clef>) [<valeur> | $<clef>] |
                                    select [<expr> | $<clef>]
				    flush)

EOF
}


file_error ()
{
    echo "No base found : $DB_FILE" >&2
    exit 1
}

set_output ()
{
    if [ ! -f "$DB_FILE" ]
    then
	echo "$DATABASE_HEADER" > "$DB_FILE"
    fi
}


db_put ()
{
    if [ -n "$1" ] && [ -n "$2" ]
    then
	KEY="$1"
	VALUE="$2"
	set_output
	if [ `echo "$VALUE" | grep '^\$.*$'` ]
	    then
	    VALUE=`echo "$VALUE" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_VALUE=`grep "^$VALUE $SEPARATOR" "$DB_FILE"`
	    if [ -z "$SEARCH_VALUE" ]
	    then
		echo "No such key : \$<$VALUE>" >&2
		exit 1
	    else
		VALUE=`echo "$SEARCH_VALUE" | sed 's/.* \([^ ]*\)$/\1/g'`
	    fi
	fi
	#### if in form $<key> ###
	if [ `echo "$KEY" | grep '^\$.*$'` ]
	    then
	    ### parse value of $key ###
	    KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	    if [ -z "$SEARCH_KEY" ]
	    then		### KEY NOT FOUND ###
		echo "No such key : \$<$KEY>" >&2
		exit 1
	    else
				### KEY FOUND ###
		KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
		SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
		if [ -z "$SEARCH_KEY" ]
		then
		    echo "$KEY $SEPARATOR $VALUE" >> "$DB_FILE"
		else
		    cat "$DB_FILE" | sed "/^$KEY $SEPARATOR/ s/[^ ]*$/$VALUE/g" > "$DB_FILE.temp"
		    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
		fi
	    fi
	    exit 0
	fi
	### KEY in form <KEY>
	SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	if [ -n "$SEARCH_KEY" ]  ### if key exists
	then                     ### Replacing key
	    cat "$DB_FILE" | sed "/^$KEY / s/[^ ]*$/$VALUE/g" > "$DB_FILE.temp"
	    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
	else                     ### Creating new key
	    echo "$KEY $SEPARATOR $VALUE" >> "$DB_FILE"
	fi
    fi
}

db_del ()
{
    KEY="$1"
    VALUE="$2"
    ### if in form $key
    if [ `echo "$KEY" | grep '^\$.*$'` ]
    then
	    ### parse value of $key ###
	KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
	SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
    fi
    if [ -z "$2" ]
    then
	### searching key
	SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	### KEY FOUND ###
	if [ -n "$SEARCH_KEY" ]
	    then
	    VALUE=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
	    cat "$DB_FILE" | sed "/^$KEY / s/[^ ]*$//g" > "$DB_FILE.temp"
	    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
	fi
    else
	### Detecting value form
    	if [ `echo "$VALUE" | grep '^\$.*$'` ]
	    then
	    VALUE=`echo "$VALUE" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_VALUE=`grep "^$VALUE $SEPARATOR" "$DB_FILE"`
	    VALUE=`echo "$SEARCH_VALUE" | sed 's/.* \([^ ]*\)$/\1/g'`
	fi
	if [ -n "$VALUE" ]
	then
	    cat "$DB_FILE" | sed "/^$KEY/d" > "$DB_FILE.temp"
	    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
	fi
    fi
    exit 0
}

db_select ()
{
    if [ ! $1 ]
    then
	cat "$DB_FILE" | sed "s/^.* $SEPARATOR //g"
	exit 0
    else
	KEY="$1"
	### case of $key
	if [ `echo "$KEY" | grep '^\$.*$'` ]
	then
	    ### parse value of $key ###
	    KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	    KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
	    SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	    if [ -z "$SEARCH_KEY" ]
	    then
		echo "No such key : \$<$KEY>" >&2
		exit 1
	    fi
	    VALUE=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
	    if [ $VERBOSE = "0" ]
	    then
		echo "<$KEY>=<$VALUE>"
	    else
		echo "$VALUE"
	    fi
	    exit 0
	else
	    if [ $VERBOSE = "0" ]
	    then
		TMP=`grep ".*$KEY.* $SEPARATOR" "$DB_FILE"`
		if [ -z "$TMP" ]
		then
		    echo "$KEY="
		else
		    grep ".*$KEY.* $SEPARATOR" "$DB_FILE" | sed "s/ $SEPARATOR /=/g"
		fi
		exit 0
	    else
		grep ".*$KEY.* $SEPARATOR" "$DB_FILE"  | sed "s/^.* $SEPARATOR //g"
	    fi
	    exit 0
	fi
    fi
}


##################################################
#### PROGRAM STARTS HERE #########################
##################################################

if [ ! "$1" ]
then
    echo "Syntax error : put" >&2
    exit $E_NOARGS
fi

while [ $# -gt 0 ]; do
    case "$1" in
	-k)
	    VERBOSE=0
	    ;;
	-c|-f)
	    DB_FILE="$2"
	    if [ -n "$2" ]
		then
		if [ ! -f "$DB_FILE" ] && [ "$1" = "-f" ]
		then
		    echo "No base found : $DB_FILE " >&2
		    exit 1
		else
		    shift
		fi
	    else
		usage
		exit $E_NOARGS
	    fi
	    ;;
	-*)
	    usage
	    exit $E_NOARGS
	    ;;
	put)
	    if [ $# -ne 3 ]
		then
		echo "Syntax error : put" >&2
		exit 1
	    else
		db_put "$2" "$3"
		shift;shift
	    fi
	    ;;
	del)
	    if [ -n "$2" ] && [ ! $4 ]
	    then
		db_del "$2" "$3"
		shift;shift
	    else
		echo "Syntax error : put" >&2
		exit $E_NOARGS
	    fi
	    ;;
	select)
	    if [ $# -lt 3 ]
	    then
		db_select "$2"
		shift
		exit 0
	    else
		echo "Syntax error : select" >&2
		exit $E_NOARGS
	    fi
	    ;;
	flush)
	    if [ "$#" -eq "1" ]
	    then
		echo "$DATABASE_HEADER" > "$DB_FILE"
		exit 0
	    else
		usage
		exit $E_NOARGS
	    fi
	    ;;
	*)
	    usage
	    break;;
    esac
    shift
done

#END

Revision: 42885
at March 12, 2011 02:32 by sp4ke


Initial Code
#!/bin/sh
## bdsh.sh for bdsh in /u/all/benzia_a/cu/temp
## Made by chakib1 benziane
## Login   <[email protected]>
##
## Started on  Fri Jan 22 22:11:26 2010 chakib1 benziane
## Last update Fri Jan 22 22:57:44 2010 chakib1 benziane
##

### BDSH V1.0
### Database Emulator with Shell

### VARIABLES ####

TRUE=0
FALSE=1
E_NOARGS=68
VERBOSE=1
DB_FILE="sh.db"
DATABASE_HEADER="BDSH DATABASE V1.0\nLast modification:   `date`\n"
SEPARATOR='			@__:::__@			'
KEY=""
VALUE=""


#################################################
####       FUNCTIONS   ##########################
#################################################


usage ()
{
    echo "Syntax error : Usage" >&2
    cat<<EOF
bdsh.sh [-k] [(-f | -c) <db_file>] (put (<clef> | $<clef>) (<valeur> | $<clef>) |
                                    del (<clef> | $<clef>) [<valeur> | $<clef>] |
                                    select [<expr> | $<clef>]
				    flush)

EOF
}


file_error ()
{
    echo "No base found : $DB_FILE" >&2
    exit 1
}

set_output ()
{
    if [ ! -f "$DB_FILE" ]
    then
	echo "$DATABASE_HEADER" > "$DB_FILE"
    fi
}


db_put ()
{
    if [ -n "$1" ] && [ -n "$2" ]
    then
	KEY="$1"
	VALUE="$2"
	set_output
	if [ `echo "$VALUE" | grep '^\$.*$'` ]
	    then
	    VALUE=`echo "$VALUE" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_VALUE=`grep "^$VALUE $SEPARATOR" "$DB_FILE"`
	    if [ -z "$SEARCH_VALUE" ]
	    then
		echo "No such key : \$<$VALUE>" >&2
		exit 1
	    else
		VALUE=`echo "$SEARCH_VALUE" | sed 's/.* \([^ ]*\)$/\1/g'`
	    fi
	fi
	#### if in form $<key> ###
	if [ `echo "$KEY" | grep '^\$.*$'` ]
	    then
	    ### parse value of $key ###
	    KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	    if [ -z "$SEARCH_KEY" ]
	    then		### KEY NOT FOUND ###
		echo "No such key : \$<$KEY>" >&2
		exit 1
	    else
				### KEY FOUND ###
		KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
		SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
		if [ -z "$SEARCH_KEY" ]
		then
		    echo "$KEY $SEPARATOR $VALUE" >> "$DB_FILE"
		else
		    cat "$DB_FILE" | sed "/^$KEY $SEPARATOR/ s/[^ ]*$/$VALUE/g" > "$DB_FILE.temp"
		    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
		fi
	    fi
	    exit 0
	fi
	### KEY in form <KEY>
	SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	if [ -n "$SEARCH_KEY" ]  ### if key exists
	then                     ### Replacing key
	    cat "$DB_FILE" | sed "/^$KEY / s/[^ ]*$/$VALUE/g" > "$DB_FILE.temp"
	    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
	else                     ### Creating new key
	    echo "$KEY $SEPARATOR $VALUE" >> "$DB_FILE"
	fi
    fi
}

db_del ()
{
    KEY="$1"
    VALUE="$2"
    ### if in form $key
    if [ `echo "$KEY" | grep '^\$.*$'` ]
    then
	    ### parse value of $key ###
	KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
	SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
    fi
    if [ -z "$2" ]
    then
	### searching key
	SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	### KEY FOUND ###
	if [ -n "$SEARCH_KEY" ]
	    then
	    VALUE=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
	    cat "$DB_FILE" | sed "/^$KEY / s/[^ ]*$//g" > "$DB_FILE.temp"
	    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
	fi
    else
	### Detecting value form
    	if [ `echo "$VALUE" | grep '^\$.*$'` ]
	    then
	    VALUE=`echo "$VALUE" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_VALUE=`grep "^$VALUE $SEPARATOR" "$DB_FILE"`
	    VALUE=`echo "$SEARCH_VALUE" | sed 's/.* \([^ ]*\)$/\1/g'`
	fi
	if [ -n "$VALUE" ]
	then
	    cat "$DB_FILE" | sed "/^$KEY/d" > "$DB_FILE.temp"
	    cat $DB_FILE.temp > $DB_FILE && rm $DB_FILE.temp
	fi
    fi
    exit 0
}

db_select ()
{
    if [ ! $1 ]
    then
	cat "$DB_FILE" | sed "s/^.* $SEPARATOR //g"
	exit 0
    else
	KEY="$1"
	### case of $key
	if [ `echo "$KEY" | grep '^\$.*$'` ]
	then
	    ### parse value of $key ###
	    KEY=`echo "$KEY" | sed 's/^\$\([^ ]*\)/\1/g'`
	    SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	    KEY=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
	    SEARCH_KEY=`grep "^$KEY $SEPARATOR" "$DB_FILE"`
	    if [ -z "$SEARCH_KEY" ]
	    then
		echo "No such key : \$<$KEY>" >&2
		exit 1
	    fi
	    VALUE=`echo "$SEARCH_KEY" | sed 's/.* \([^ ]*\)$/\1/g'`
	    if [ $VERBOSE = "0" ]
	    then
		echo "<$KEY>=<$VALUE>"
	    else
		echo "$VALUE"
	    fi
	    exit 0
	else
	    if [ $VERBOSE = "0" ]
	    then
		TMP=`grep ".*$KEY.* $SEPARATOR" "$DB_FILE"`
		if [ -z "$TMP" ]
		then
		    echo "$KEY="
		else
		    grep ".*$KEY.* $SEPARATOR" "$DB_FILE" | sed "s/ $SEPARATOR /=/g"
		fi
		exit 0
	    else
		grep ".*$KEY.* $SEPARATOR" "$DB_FILE"  | sed "s/^.* $SEPARATOR //g"
	    fi
	    exit 0
	fi
    fi
}


##################################################
#### PROGRAM STARTS HERE #########################
##################################################

if [ ! "$1" ]
then
    echo "Syntax error : put" >&2
    exit $E_NOARGS
fi

while [ $# -gt 0 ]; do
    case "$1" in
	-k)
	    VERBOSE=0
	    ;;
	-c|-f)
	    DB_FILE="$2"
	    if [ -n "$2" ]
		then
		if [ ! -f "$DB_FILE" ] && [ "$1" = "-f" ]
		then
		    echo "No base found : $DB_FILE " >&2
		    exit 1
		else
		    shift
		fi
	    else
		usage
		exit $E_NOARGS
	    fi
	    ;;
	-*)
	    usage
	    exit $E_NOARGS
	    ;;
	put)
	    if [ $# -ne 3 ]
		then
		echo "Syntax error : put" >&2
		exit 1
	    else
		db_put "$2" "$3"
		shift;shift
	    fi
	    ;;
	del)
	    if [ -n "$2" ] && [ ! $4 ]
	    then
		db_del "$2" "$3"
		shift;shift
	    else
		echo "Syntax error : put" >&2
		exit $E_NOARGS
	    fi
	    ;;
	select)
	    if [ $# -lt 3 ]
	    then
		db_select "$2"
		shift
		exit 0
	    else
		echo "Syntax error : select" >&2
		exit $E_NOARGS
	    fi
	    ;;
	flush)
	    if [ "$#" -eq "1" ]
	    then
		echo "$DATABASE_HEADER" > "$DB_FILE"
		exit 0
	    else
		usage
		exit $E_NOARGS
	    fi
	    ;;
	*)
	    usage
	    break;;
    esac
    shift
done

#END

Initial URL

                                

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

Initial Title
Light sql like database written in Bash

Initial Tags
database, sql, Bash, script

Initial Language
Bash