Why are $0, ~, &c. used in a way that violates usu

2019-07-24 21:26发布

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)\
             }'\
 )

标签: bash shell awk
2条回答
做个烂人
2楼-- · 2019-07-24 22:10

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:

  1. We are setting the variable ids to the following
  2. Take the output from xinput --list and pipe it to awk
  3. Before starting the awk script set awk variable search to shell variable $SEARCH. So a $SEARCH needs to be set first. -v search="$SEARCH"
  4. Processing each record, test to see if anywhere in the entire record ($0) the value stored in variable search is found. If so, proceed.
  5. Using match() test the entire record ($0) again, but this time with regex string id=[0-9]+ which would be a string containing id= followed by 1 or more numbers. Match will set awk variable RSTART with the starting position of the match.
  6. if RSTART is set, then a match was found, so...
  7. Print the substring of the entire record ($0) starting three characters after match was found (in this case after id=) for the length of the match RLENGTH minus 3 characters. Essentially whatever numbers are found after the id.

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 an id=<some numbers> on the same line of the xinput --list output and return those numbers.

An example:

If xinput --list spits out the following two lines:

This is the first line of xinput --line and the id=12345
This is the second line of xinput --line and the id=67891

If you set SEARCH="second" and then executed this statement it would output 67891 and store it in variable ids.

查看更多
放荡不羁爱自由
3楼-- · 2019-07-24 22:16

awk is a different programming language, with its own syntax. The string being passed to awk 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.
查看更多
登录 后发表回答