-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy
executable file
·103 lines (90 loc) · 2.5 KB
/
my
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
#!/bin/sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2021 Jeremy Brubaker <jbru362@gmail.com>
#
# abstract: execute the correct command from a list of alternatives
#
MY_DIR=$HOME/etc/my
case "$1" in
-v)
v='y'
shift
;;
-d)
shift
if ! test -d "$1"; then
printf "%s: not a directory\n", "$1" >&2
return 2>/dev/null || exit
fi
MY_DIR="$1"
shift
;;
-h)
cat <<EOF
$0: $0 [-dv] category [arg ...]
Execute the correct command from a list of alternatives or display
information about commands.
Options:
-d dir look for alternatives lists in DIR (default: $MY_DIR)
-v print the selected command
Exit Status:
Returns exit status of COMMAND, or failure if COMMAND is not found.
Alternative list format:
Each file should be named <category> and contain one entry per line. Each
entry is the command to be executed. An optional tag 'X' may follow,
separated by a comma. Entries tagged thus, will be ignored if \$DISPLAY is
not defined.
Example:
# browser
firefox,X
chrome,X
links
lynx
EOF
return 2>/dev/null || exit
;;
-*)
printf "%s: %s: invalid option\n" "$0" "$1" >&2
return 2>/dev/null || exit
;;
esac
f="$MY_DIR/$1"
if ! test -r "$f"; then
printf "Category '%s' not found\n" "$1" >&2
return 2>/dev/null || exit
fi
shift
while IFS=',' read -r cmd tag; do
if test "$tag" == "X" && test -z "$DISPLAY"; then
continue
fi
if command -v "$cmd" >/dev/null 2>&1; then
break
fi
done < "$f"
if test -z "$cmd"; then
printf "No alternative found for 'category' %s\n" "$1" >&2
fi
if test "$v" == "y"; then
echo "$cmd"
else
if test "$tag" == "X"; then
(exec $cmd "$@" &)
else
$cmd "$@"
fi
fi
return 2>/dev/null || exit