@@ -27,19 +27,12 @@ _db.iskey(){
27
27
# file (str) > takes file path.
28
28
db.read (){
29
29
# checking args given or not.
30
- if [[ ! ${# } -eq 2 ]]; then
31
- echo " error: 'missing args'" ;
32
- return 1;
33
- fi
34
- if _db.iskey " ${1} " " ${2} " ; then
35
- local dbValue;
36
- dbValue=" $( grep " ${1} : " " ${2} " ) " ;
37
- echo " ${dbValue} " | awk -F' : ' ' {sub(/^[^:]*: /, ""); print}' ;
38
- return 0;
39
- else
40
- echo " error: ${1} : 'key not exists'" ;
41
- return 1;
42
- fi
30
+ [[ ${# } -eq 2 ]] ||
31
+ { echo " error: 'missing args'" && return 1; };
32
+ _db.iskey " ${1} " " ${2} " ||
33
+ { echo " error: ${1} : 'key not exists'" && return 1; };
34
+ local dbValue=" $( grep " ${1} : " " ${2} " ) " ;
35
+ echo " ${dbValue} " | awk -F' : ' ' {sub(/^[^:]*: /, ""); print}' ;
43
36
}
44
37
45
38
# db.create(key,value,file)
@@ -50,17 +43,11 @@ db.read(){
50
43
# file (str) > takes file path.
51
44
db.create (){
52
45
# checking args given or not.
53
- if [[ ! ${# } -eq 3 ]]; then
54
- echo " error: 'missing args'" ;
55
- return 1;
56
- fi
57
- if _db.iskey " ${1} " " ${3} " ; then
58
- echo " error: ${1} : 'key already exist'" ;
59
- return 1;
60
- else
61
- echo -ne " \n${1} : ${2} " >> " ${3} " ;
62
- return 0;
63
- fi
46
+ [[ ${# } -eq 3 ]] ||
47
+ { echo " error: 'missing args'" && return 1; };
48
+ _db.iskey " ${1} " " ${3} " &&
49
+ { echo " error: ${1} : 'key already exists'" && return 1; };
50
+ echo -ne " \n${1} : ${2} " >> " ${3} " ;
64
51
}
65
52
66
53
# db.update(key,value,file)
@@ -71,33 +58,26 @@ db.create(){
71
58
# file (str) > takes file path.
72
59
db.update (){
73
60
# checking args given or not.
74
- if [[ ! ${# } -eq 3 ]]; then
75
- echo " error: 'missing args'" ;
76
- return 1;
77
- fi
78
- if _db.iskey " ${1} " " ${3} " ; then
79
- # taking key arg.
80
- local dbKey=" ${1} " ;
81
- # taking update value of given key.
82
- local dbUpdatedValue=" ${2} " ;
83
- # taking db file path.
84
- local dbFile=" ${3} " ;
85
- # getting old value of given key.
86
- local dbValue;
87
- dbValue=" $( db.read " ${dbKey} " " ${dbFile} " ) " ;
88
- # concating key and old value.
89
- local dbKeyValuePair=" ${dbKey} : ${dbValue} " ;
90
- # getting position of concated line in file.
91
- local dbKeyValuePairPos;
92
- dbKeyValuePairPos=" $( grep -n " ${dbKeyValuePair} " " ${dbFile} " | cut -d: -f1 | head -n 1; ) " ;
93
- # replacing value from old to new.
94
- local dbUpdatedKeyValuePair=" ${dbKeyValuePair/ ${dbValue} / " ${dbUpdatedValue} " } " ;
95
- # placing updated value to position.
96
- sed -i " ${dbKeyValuePairPos} c\\ ${dbUpdatedKeyValuePair} " " ${dbFile} " ;
97
- else
98
- echo " error: ${1} : 'key not exists'" ;
99
- return 1;
100
- fi
61
+ [[ ${# } -eq 3 ]] ||
62
+ { echo " error: 'missing args'" && return 1; };
63
+ _db.iskey " ${1} " " ${3} " ||
64
+ { echo " error: ${1} : 'key not exists'" && return 1; };
65
+ # taking key arg.
66
+ local dbKey=" ${1} " ;
67
+ # taking update value of given key.
68
+ local dbUpdatedValue=" ${2} " ;
69
+ # taking db file path.
70
+ local dbFile=" ${3} " ;
71
+ # getting old value of given key.
72
+ local dbValue=" $( db.read " ${dbKey} " " ${dbFile} " ) " ;
73
+ # concating key and old value.
74
+ local dbKeyValuePair=" ${dbKey} : ${dbValue} " ;
75
+ # getting position of concated line in file.
76
+ local dbKeyValuePairPos=" $( grep -n " ${dbKeyValuePair} " " ${dbFile} " | cut -d: -f1 | head -n 1; ) " ;
77
+ # replacing value from old to new.
78
+ local dbUpdatedKeyValuePair=" ${dbKeyValuePair/ ${dbValue} / " ${dbUpdatedValue} " } " ;
79
+ # placing updated value to position.
80
+ sed -i " ${dbKeyValuePairPos} c\\ ${dbUpdatedKeyValuePair} " " ${dbFile} " ;
101
81
}
102
82
103
83
# db.delete(key,file)
@@ -107,18 +87,12 @@ db.update(){
107
87
# file (str) > takes file path.
108
88
db.delete (){
109
89
# checking args given or not.
110
- if [[ ! ${# } -eq 2 ]]; then
111
- echo " error: 'missing args'" ;
112
- return 1 ;
113
- fi
90
+ [[ ${# } -eq 2 ]] ||
91
+ { echo " error: 'missing args'" && return 1 ; } ;
92
+ _db.iskey " ${1} " " ${2} " ||
93
+ { echo " error: ${1} : 'key not exists' " && return 1 ; } ;
114
94
local dbKey=" ${1} : " ;
115
95
local dbFile=" ${2} " ;
116
- if _db.iskey " ${1} " " ${2} " ; then
117
- local dbKeyPos;
118
- dbKeyPos=" $( grep -n " ${dbKey} " " ${dbFile} " | cut -d: -f1 | head -n 1; ) " ;
119
- sed -i " ${dbKeyPos} d" " ${dbFile} " ;
120
- else
121
- echo " error: ${1} : 'key not exists'" ;
122
- return 1;
123
- fi
96
+ local dbKeyPos=" $( grep -n " ${dbKey} " " ${dbFile} " | cut -d: -f1 | head -n 1; ) " ;
97
+ sed -i " ${dbKeyPos} d" " ${dbFile} " ;
124
98
}
0 commit comments