-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbrep.1.3
79 lines (78 loc) · 2.1 KB
/
dbrep.1.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh
# File: dbrep.1.3
# System: D.B.SHDB.1
# Dependencies: -
# ------------------------------------------------
# Author: Daniel Bullimore
# Authors Email: -
# First Written: 2013
# ------------------------------------------------
# !If you make changes add your name to the list
# Last Modified (d/m/y):
# 4/5/2019 Daniel Bullimore
# 9/5/2019 daniel bullimore
# 11/5/2019 daniel bullimore
# ------------------------------------------------
# Copyright 2019 Daniel Bullimore
# D.B.SHDB is licensed under the The 3-Clause BSD License
# To view this license:
# https://github.com/DanielBullimore/D.B.SHDB/blob/master/LICENSE
#
#--------------
#---DISCRIPTION\
#---------------
# Replaces the value of given key in database file
# Checks null parameters, database exists and can be read/written
# Insures no other replace functions are in action
# exits on 1 on parameter/state errors
# reads database file line by line searching for replace key
# writes all non matching pairs to temp file
# once found the matching key is written to temp with new value
# when all lines copied a check is made for the replacement having taken place
# over writes database file with new temp file if replacement took place
# returns error if replace key was not matched.
#------------------
# Notes
#------------------
# Unique/random temp file name would allow more than one replace case to be run at the same time.
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]
then
echo "Syntax: db rep <row_key> <new_value> <db_file>";
exit 1;
elif [ ! -f "$3" ]
then
echo "Error: database not found";
exit 1;
elif [ ! -r "$3" ] || [ ! -w "$3" ]
then
echo "Error: can not read or write from database file: $3";
exit 1;
else
if [ ! -f "$3.temp" ]
then
touch "$3.temp";
else
echo "Error: File IO colision";
exit 1;
fi;
FOUND="FALSE";
while read f
do
KEY=$(echo "$f" | cut -d\ -f1);
if [ "$KEY" = "$1" ]
then
#found=>
echo "$1 $2" >> "$3.temp";
FOUND="TRUE";
else
echo "$f" >> "$3.temp";
fi;
done < "$3";
if [ "$FOUND" = "TRUE" ]
then
mv "$3.temp" "$3";
else
echo "Error: Key not found";
exit 1;
fi;
fi;