-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpm.sh
More file actions
executable file
·93 lines (80 loc) · 2.04 KB
/
Copy pathpm.sh
File metadata and controls
executable file
·93 lines (80 loc) · 2.04 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
obj_001_001k="name"
obj_001_001v="entry 1"
obj_001_002k="url"
obj_001_002v="http://example.com"
obj_002_001k="name"
obj_002_001v="entry 2"
obj_003_001k="name"
obj_003_001v="entry 3"
main() {
FN_PREFIX="impl_"
OBJ_PREFIX="obj_"
impl_exit() {
[ "$1" = "help" ] && {
echo "exit"
echo " will exit program"
return
}
exit
}
impl_help() {
[ "$1" = "help" ] && {
echo "help"
echo " show help for all commands"
return
}
local fn
for fn in $(compgen -A function|grep "^${FN_PREFIX}"|sort); do
$fn help
done
}
impl_ls() {
[ "$1" = "help" ] && {
echo "ls"
echo " list entries"
return
}
local name_key name_id
while read name_key; do
name_id=$(cut -f2 -d_ <<<$name_key)
printf "%s: %s\n" "$name_id" "${!name_key}"
done < <(set|grep "^${OBJ_PREFIX}[^=]*k=name$"|sed -e 's/k=.*/v/')
}
impl_cat() {
[ "$1" = "help" ] && {
echo "cat <entry_id>"
echo " show entry content"
return
}
local entry_id=$1 key value
while read key; do
value=${key/k/v}
printf "%s: %s\n" "${!key}" "${!value}"
done < <(set|grep -oP "^${OBJ_PREFIX}${entry_id}_\d+k")
}
impl_rm() {
[ "$1" = "help" ] && {
echo "rm <entry_id>"
echo " delete entry"
return
}
local entry_id=$1 var
while read var; do
unset $var
done < <(set|grep -oP "^${OBJ_PREFIX}${entry_id}_[^=]+")
}
at_exit() {
echo "at_exit"
}
trap at_exit EXIT
local input
while read -e -p "> " -a input; do
[ "$(type -t "${FN_PREFIX}${input[0]}")" != "function" ] && {
echo "Error: unknown command \"${input[0]}\""
input=(help)
}
"${FN_PREFIX}${input[0]}" "${input[@]:1}"
done
}
main "$@"