@@ -27,19 +27,12 @@ _db.iskey(){
2727# file (str) > takes file path.
2828db.read (){
2929 # 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}' ;
4336}
4437
4538# db.create(key,value,file)
@@ -50,17 +43,11 @@ db.read(){
5043# file (str) > takes file path.
5144db.create (){
5245 # 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} " ;
6451}
6552
6653# db.update(key,value,file)
@@ -71,33 +58,26 @@ db.create(){
7158# file (str) > takes file path.
7259db.update (){
7360 # 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} " ;
10181}
10282
10383# db.delete(key,file)
@@ -107,18 +87,12 @@ db.update(){
10787# file (str) > takes file path.
10888db.delete (){
10989 # 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 ; } ;
11494 local dbKey=" ${1} : " ;
11595 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} " ;
12498}
0 commit comments