In trying to understand and apply the accepted answer to How to make a program that finds ids of XInput devices, I'm finding code that doesn't appear to comply with the documented syntax for bash.
~
is documented as bitwise negation -- a mathematical operator taking only one argument -- but it's being run here with$0
as one argument and a string as the other.$0
is documented as referring to the name that the script itself was started with, but referring to that name doesn't make sense in the context of what this code is supposed to do.
What am I missing here?
ids=$(xinput --list | awk -v search="$SEARCH" \
'$0 ~ search {match($0, /id=[0-9]+/);\
if (RSTART) \
print substr($0, RSTART+3, RLENGTH-3)\
}'\
)
I was going to post this on your follow up question which has been flagged as duplicate. You asked in that question what this script was doing. This entire script can be broken down like:
ids
to the followingxinput --list
and pipe it to awksearch
to shell variable$SEARCH
. So a$SEARCH
needs to be set first.-v search="$SEARCH"
$0
) the value stored in variablesearch
is found. If so, proceed.match()
test the entire record ($0
) again, but this time with regex stringid=[0-9]+
which would be a string containingid=
followed by 1 or more numbers. Match will set awk variableRSTART
with the starting position of the match.$0
) starting three characters after match was found (in this case afterid=
) for the length of the matchRLENGTH
minus 3 characters. Essentially whatever numbers are found after theid
.So stating this in english. Set variable $SEARCH to something that you are searching for in the output of
xinput --list
. This awk script will find the line that matches that search string. It will look for anid=<some numbers>
on the same line of thexinput --list
output and return those numbers.An example:
If
xinput --list
spits out the following two lines:If you set
SEARCH="second"
and then executed this statement it would output67891
and store it in variableids
.awk
is a different programming language, with its own syntax. The string being passed toawk
is thus parsed as code in the awk language, rather than as bash.In awk (but not bash):
$0
refers to the complete text of the line being processed.~
is a regex operator;text ~ regex
is a boolean operation which has a truthy result if the text matches the regex.