-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhichELF?
179 lines (172 loc) · 8.12 KB
/
whichELF?
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
info='
"whichELF?" replacement for type/which by oOosys created: 2024-02-24_22:47
VERSION 2.01 last update: 2024-03-31_14:07
Follow the chain of links and scripts to report the actual executable
or find an executable describing its functionality.
A tool for use in own self-created custom part of the oOo system able to reveal how
deep the indirect calls of an executable in a modern Linux system can be
or to find an executable listing provided keywords in its man/info description.
USAGE:
~ $ whichELF? nameOfCommandExecutable
~ $ wchicELF? keyword_1 keyword_2 [keyword_3 ...]
~ $ whichELF? oneKeywordOnly " "
'
if [ "$#" -eq 0 ]; then # list of parameter is empty, but at least one is required, so:
echo "$info" >&2
exit 1
fi
if [ "$#" -gt 1 ]; then # there are two or more parameter, so perform a keyword search:
regex=""
keywPrefix='(?=.*?' # non-greedy look ahead regex pattern prefix
keywPostfix=')' # closing bracket to opening bracket of prefix
for keyword in "$@"; do # append each keyword to the regex pattern
keyword=${keyword/\*/\\*}
keyword=${keyword/(/\\(}
keyword=${keyword/)/\\)}
keyword=${keyword/^/\\^}
keyword=${keyword/$/\\$}
keyword=${keyword/./\\.}
keyword=${keyword/[/\\[}
keyword=${keyword/]/\\]}
regex="${regex}${keywPrefix}${keyword}${keywPostfix}"
done
printf 'Searching for executable description matching regex: "%s"' $regex; echo
apropos . | grep --ignore-case --color=always --perl-regexp "${regex}" | less -R
exit 0
fi
if :; then
fbn=$1 # fbn -> acronym -> file base name
echo " $fbn"
fpn=$(which $fbn) # ... check if executable in $PATH , fpn -> file/full path name
if [ ! -e "$fpn" ] ; then # failed to find an executable file with this name so ...
# the shell builtin type eventually raises an error so it needs to be handled ...
# ... in order to achieve customized output describing the outcome of type :
handleError () { echo "$indent-> ISSUE: $fbn not found"; exit; } # ... assures that there will be customized n output even in case of an error ...
trap 'handleError "$BASH_COMMAND"' ERR # bash specific error handling
typeType=$(type $fbn 2> /dev/null) # ... use type and catch an error if any
echo "$indent-> $typeType " # ... <- type succeeded, echo the result ...
indent=" $indent"
echo "$indent->MAYBE the current shell is: $SHELL (???)" # ... fails e.g. in fish ...
s2=$(file $SHELL 2>/dev/null) s3=${s2##*:} indent=" $indent"
SIZE=$(stat -c%s "$SHELL") # provide the file size of the executable
echo "$indent->$s3"; indent=" $indent"
echo "$indent( fileSize = $SIZE bytes )"
exit
fi
indent=" " # for use as next output lines indentation
typeType=$(type $fbn 2> /dev/null) # ... check type ( function, builtin, ...) NOTICE: aliases are not inherited so won't be detected here!
if [ -n "$fpn" ] && [ ! -e "${typeType##* }" ]; then # $1 is both: executable and another type so ...
echo "$indent-> $typeType " # ... <- type succeeded, echo the result
echo "--- and:"
fi
echo "$indent-> $fpn"
BCKPCWD=$(pwd) # backup current working directory (for restore later on)
while : ; do
DIR="$(dirname "${fpn}")" # FILE="$(basename "${fpn}")"
SIZE=$(stat -c%s "$fpn") # provide file size of the executable
s2=$(file $fpn 2>/dev/null) s3=${s2##*:} indent=" $indent"
echo "$indent-> $s3"
echo "$indent ( fileSize = $SIZE bytes )"
s4=${s3##* } # get the last column with the file name path
cd $DIR # required for symbolic link with relative path
# use $(pwd) to obtain the full path name of current directory
if [ ! -e "$s4" ] ; then
if [[ $s3 == *"text"* ]]; then
scriptShebang=$(head -n 1 $fpn)
if [[ "$(printf "$scriptShebang"|head -c2)" == "#!" ]]; then
scriptInterpreterPath=$( echo $scriptShebang | cut --delimiter='!' --fields=2 )
echo $scriptShebang
whichf $scriptInterpreterPath
fi
indent=" $indent"
s5=${s2%%:*}
echo "$indent-> LAST SCRIPT LINE (may list the actual used or further executable):"
indent=" $indent"
echo -e "$indent-> $(cat $s5 | tail -n1)"
fi
cd $BCKPCWD # restore current working directory
break # exit the loop and the script
fi
fpn=$s4
done
fi
examplesOfUsage_includingStdout='
========================================================================
~ $ whichELF? pwd
pwd
-> pwd is a shell builtin
--- and:
-> /usr/bin/pwd
-> ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=cf610477f0530f1dbeac1ed8f650f869fb610d10, for GNU/Linux 3.2.0, stripped
( fileSize = 35328 bytes )
~ $ whichELF? echo
echo
-> echo is a shell builtin
--- and:
-> /usr/bin/echo
-> ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=b56baf290970e98b8056b1a8114a4404b8186689, for GNU/Linux 3.2.0, stripped
( fileSize = 35120 bytes )
========================================================================
~ $ whichELF? which
which
-> /usr/bin/which
-> symbolic link to /etc/alternatives/which
( fileSize = 23 bytes )
-> symbolic link to /usr/bin/which.debianutils
( fileSize = 26 bytes )
-> POSIX shell script, ASCII text executable
( fileSize = 946 bytes )
#! /bin/sh
/bin/sh
-> /bin/sh
-> symbolic link to dash
( fileSize = 4 bytes )
-> ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=f7ab02fc1b8ff61b41647c1e16ec9d95ba5de9f0, for GNU/Linux 3.2.0, stripped
( fileSize = 125688 bytes )
-> LAST SCRIPT LINE (may list the actual used or further executable):
-> exit "$ALLRET"
========================================================================
~ $ whichELF? type
type
-> type is a shell builtin
->MAYBE the current shell is: /bin/bash (???)
-> ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=33a5554034feb2af38e8c75872058883b2988bc5, for GNU/Linux 3.2.0, stripped
( fileSize = 1396520 bytes )
========================================================================
~ $ whichELF? whichELF?
whichELF?
-> /usr/local/sbin/whichELF?
-> symbolic link to /home/o/oOo/ooo/root/usr/local/sbin/whichELF?
( fileSize = 45 bytes )
-> Bourne-Again shell script, ASCII text executable
( fileSize = 3445 bytes )
#!/bin/bash
/bin/bash
-> /bin/bash
-> ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=33a5554034feb2af38e8c75872058883b2988bc5, for GNU/Linux 3.2.0, stripped
( fileSize = 1396520 bytes )
-> LAST SCRIPT LINE (may list the actual used or further executable):
-> # -- END of whichELF? shell script executing: cd,echo,file,stat,type,which --
========================================================================
~ $ whichELF? view
view
-> /usr/bin/view
-> symbolic link to /etc/alternatives/view
( fileSize = 22 bytes )
-> symbolic link to /usr/libexec/neovim/view
( fileSize = 24 bytes )
-> POSIX shell script, ASCII text executable
( fileSize = 37 bytes )
#!/bin/sh
/bin/sh
-> /bin/sh
-> symbolic link to dash
( fileSize = 4 bytes )
-> ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=f7ab02fc1b8ff61b41647c1e16ec9d95ba5de9f0, for GNU/Linux 3.2.0, stripped
( fileSize = 125688 bytes )
-> LAST SCRIPT LINE (may list the actual used or further executable):
-> exec /usr/bin/nvim -R "$@"
========================================================================
'
# -- LAST LINE -> by script used commands/executables: cd, echo, file, stat, type, which --