Skip to content

Commit 5407b89

Browse files
committed
code shorten
1 parent 81600d2 commit 5407b89

File tree

3 files changed

+43
-75
lines changed

3 files changed

+43
-75
lines changed

src/db.sh

+37-63
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,12 @@ _db.iskey(){
2727
# file (str) > takes file path.
2828
db.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.
5144
db.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.
7259
db.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.
10888
db.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
}

src/file.sh

+4-8
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,8 @@ file.append.hori(){
192192
# takes file as arg.
193193
local ARGFile="${3}";
194194
# checking args given or not.
195-
if [[ ! ${#} -eq 3 ]]; then
196-
echo "error: 'missing args'";
197-
return 1;
198-
fi
195+
[[ ${#} -eq 3 ]] ||
196+
{ echo "error: 'missing args'" && return 1; };
199197
# reading line from given position.
200198
local Vir="$(file.readline "${Pos}" "${ARGFile}")";
201199
# concating string to it.
@@ -220,10 +218,8 @@ file.append.vert(){
220218
# takes file as arg.
221219
local ARGFile="${3}";
222220
# checking args given or not.
223-
if [[ ! ${#} -eq 3 ]]; then
224-
echo "error: 'missing args'";
225-
return 1;
226-
fi
221+
[[ ${#} -eq 3 ]] ||
222+
{ echo "error: 'missing args'" && return 1; };
227223
# reduce one from variable.
228224
local Pos="$(( Pos - 1 ))";
229225
# do operation.

src/package.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ source "${Dir}"/os.sh
3030
# pkg.size(install,package) > Gives you package installed size.
3131
pkg.size(){
3232
# checking args given or not.
33-
if [[ ! ${#} -eq 2 ]]; then
34-
echo "error: 'missing args'";
35-
return 1;
36-
fi
33+
[[ ${#} -eq 2 ]] ||
34+
{ echo "error: 'missing args'" && return 1; };
3735
case "${1}" in
3836
'dnload') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Download-Size:')";;
3937
'install') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Installed-Size:')";;

0 commit comments

Comments
 (0)