-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.sh
executable file
·149 lines (128 loc) · 2.75 KB
/
aliases.sh
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env dash
#
# Copyright (C) 2024 Maria Lisina
# Copyright (C) 2024 Danil Lisin
# SPDX-License-Identifier: Apache-2.0
#
# Run this software with `env -i` to avoid variable conflict
set -e
umask 77
lists="${0%/*}/lists"
list="${lists}/aliases"
if [ -n "${1}" ]
then
action="${1}"
case "${action}" in
(help)
help=0
;;
(show)
show=0
;;
(add | del)
;;
(*)
echo "Unrecognized action ${action}" \
"\nSee '${0} help'"
exit 1
;;
esac
shift
else
help=0
fi
if [ -n "${help}" ]
then
echo "Aliases Manager" \
"\n\nUsage: ${0} [action] [ID] [alias]" \
"\n\nActions:" \
"\n help\t\tShow help information" \
"\n show\t\tShow all aliases" \
"\n add\t\tAdd user ID alias" \
"\n del\t\tRemove user ID alias"
exit 0
fi
for required in busybox
do
if ! command -v ${required} > /dev/null
then
missing="${missing} ${required}"
fi
done
if [ -n "${missing}" ]
then
echo "Missing dependencies:${missing}" \
"\nFor more information follow: https://command-not-found.com/"
exit 1
fi
for function in cat grep mkdir sed
do
if busybox ${function} --help > /dev/null 2>&1
then
alias ${function}="busybox ${function}"
else
missing="${missing} ${function}"
fi
done
if [ -n "${missing}" ]
then
echo "Missing BusyBox functions:${missing}" \
"\nUpdate your BusyBox or get a version with all the required functions"
exit 1
fi
if [ -n "${show}" ]
then
if [ -f "${list}" ]
then
cat "${list}"
fi
exit 0
fi
if [ -n "${1}" ]
then
user_id="${1}"
if ! test ${user_id} -gt 0 > /dev/null 2>&1
then
echo "Illegal user ID ${user_id}"
exit 1
fi
shift
else
echo "You must specify the target user ID" \
"\nSee '${0} help'"
exit 1
fi
mkdir -p "${config}"
case "${action}" in
(add)
if [ -n "${1}" ]
then
alias="${1}"
shift
else
echo "You must specify the alias name" \
"\nSee '${0} help'"
exit 1
fi
if [ -s "${list}" ]
then
set -- $(cat "${list}")
while [ ${#} -ge 2 ]
do
if [ "${1}" = "${user_id}" ]
then
echo "User ID ${user_id} already has an alias ${2}"
exit 1
fi
shift 2
done
fi
printf "%s %s\n" "${user_id}" "${alias}" >> "${list}"
;;
(del)
if [ -f "${list}" ]
then
sed -e "s/${user_id} .*$//" -e '/^$/d' -i "${list}"
fi
;;
esac