/
bbc-cmd-hub
executable file
·137 lines (116 loc) · 3.63 KB
/
bbc-cmd-hub
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
# ==============================================================================
# COMMAND HUB
# A tool to automatically create a selection interface from a list of commands.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# ==============================================================================
function show_help () {
cat << EOF
Create a selection interface from a list of commands.
Usage: $( basename $0) [-i <CMD_FILE>] [-c <CMD_LIST>] [-t <HEADER_FILE>] [-s]
-i <CMD_FILE> Input file holding bash commands.
-c <CMD_LIST> Input command generating list entries.
-t <HEADER_FILE> An optional file holding a header to be printed.
-p <PREFIX> Optional command prefix (eg. 'java -jar')
-s <SUFFIX> Optional command suffix (eg. '-Xmx1024m')
-n No whitespaces before/after suffix/prefix
-d Don't clear screen
Note that either -i or -c must be set.
EOF
exit 1
}
# setup temp files and ensure deletion on exit
tmplist=$( mktemp )
strippedlist=$( mktemp )
function cleanup () {
rm -f $tmplist $strippedlist
}
trap cleanup EXIT SIGTERM
# setup coloring if tput available
hl=
rs=
if [ ! -z $( command -v tput ) ]; then
hl=$( tput setaf 7; tput setab 4; )
hr=$( tput setaf 4; )
rs=$( tput sgr0 )
fi
# parse cmd line
while getopts "hi:t:c:p:s:dn" opt; do
case "$opt" in
i) INFILE="$OPTARG";;
t) HFILE="$OPTARG";;
c) CMDLIST="$OPTARG";;
p) PREFIX="$OPTARG";;
s) SUFFIX="$OPTARG";;
d) DONTCLEAR=1 ;;
n) NOWS=1 ;;
h) show_help;;
*) echo "Illegal argument."; show_help;;
esac
done
# check if there is a remaining argument
shift $(expr $OPTIND - 1 )
# if it's a number use it as preselection
[ $# -gt 0 ] && [[ "$1" =~ ^[0-9]+$ ]] && PRESELECTION="$1"
# check settings
[ -z "$INFILE" ] && [ -z "$CMDLIST" ] && { echo "No input."; show_help; }
[ ! -z "$INFILE" ] && [ ! -e "$INFILE" ] && {
echo "Input file does not exist."; show_help; }
# ==============================================================================
# MAIN PROCESSING
# ==============================================================================
[ ! -z "$INFILE" ] && cat "$INFILE" | grep -v -e "^[[:space:]]$" \
>> $tmplist # add cmds from file
[ ! -z "$CMDLIST" ] && eval "$CMDLIST" | grep -v -e "^[[:space:]]$" \
>> $tmplist # add cmds from command
# strip away leading characters that are equal to all commands
awk 'NR==1{
pos=split($0,a,"")
}
NR==FNR{
split($0,b,"")
for(i=1;i<=pos;i++)
if(b[i]!=a[i]){
pos=i
break
}
next
}
NR!=FNR{
print substr($0,pos)
}' $tmplist $tmplist > $strippedlist
[ ! $DONTCLEAR ] && printf "\033c"
[ ! -z "$HFILE" ] && { cat $HFILE; echo ""; } # header if available
i=1
[ ! -z "$PREFIX" ] && echo -e "${hr}${PREFIX} …${rs}"
while read line
do
printf "$hl[%03s%s]$rs\t${line}\n" $i
i=$(($i + 1))
done < "$strippedlist"
[ ! -z "$SUFFIX" ] && echo -e "${hr}… ${SUFFIX}${rs}"
# if there is a preselection, just run it ..
if [ ! -z $PRESELECTION ]
then
selection=$PRESELECTION
else
# .. otherwise let user select
echo
echo -n "Enter selection: "
read -r -e -n 2 selection
echo
fi
sel_line=$( cat $tmplist | sed -n ${selection}p )
# echo $sel_line; sleep 3
if [ -z "$sel_line" ]
then
echo "Invalid selection"
exit 1
fi
jump="$PREFIX $( echo "$sel_line" | sed -e "s/&/\\\\&/g" ) $SUFFIX" # escape &
if [ ! -z "$NOWS" ]; then # No whitespaces between prefix, payload and suffix
jump="$PREFIX$( echo "$sel_line" | sed -e "s/&/\\\\&/g" )$SUFFIX"
fi
[ ! $DONTCLEAR ] && printf "\033c"
echo "Calling command: $jump"
eval "$jump"