refactor: simplify sed/awk/grep uses, etc.#1574
Conversation
The variable "prefix" is explicitly initialized when it is declared, so we do not need to explicitly set it to an empty string on this line.
The current implementation does not work for several reasons.
* First, for the selection "|" to work, one needs to enclose the
entire pattern with "@()". The option argument of the "-X" option is
not a regular expression, but an extended glob pattern.
* Next, the option argument of the "-X" option should not be singly
quoted in the present case because command substitutions in the
option argument of "-X" is not going to be expanded.
* Also, one also wants to quote the word of the here string. In Bash
< 4.4, the word of the here string is subject to word splitting, so
it may be affected by IFS.
In addition, we actually do not need to use the "tr" command. We can
reduce the spawn cost by using "${words[*]}" with an appropriate value
of IFS.
"_comp_cmd_tshark__{prefs,protocols}" are mutable global variables
used to cache the prefixes and protocols, so they should have "_mut_"
in the variable names.
| options=$(module help 2>&1 | command grep -E '^[[:space:]]*\+' | | ||
| _comp_awk '{print $2}' | command sed -e 's/|/ /g' | sort) | ||
| options=$(module help 2>&1 | _comp_awk '/^[[:space:]]*\+/ { | ||
| $0 = $2; gsub(/\|/, " "); print }' | sort) |
There was a problem hiding this comment.
I have a nagging feeling that there is an awk implementation in existence that does not support gsub, but I can't find it right now and gsub is in POSIX at least down to 2008, so maybe it's all good.
There is one gsub use in the tree in chronyc, but that could be fine if we assume chronyc is Linux specific.
There was a problem hiding this comment.
Isn't that Solaris awk? Solaris <= 11.3 awk indeed doesn't have gsub:
$ awk 'gsub(/a/, "b")' <<< "aaazzz"
awk: syntax error near line 1
awk: bailing out near line 1We use /usr/xpg4/bin/awk (which has gsub) in Solaris through the wrapper _comp_awk, so the problem doesn't happen in Solaris.
I don't have AIX, NetBSD, and OpenBSD, but online manuals seem to suggest that their awk has gsub.
- aix/7.1.0 awk (Mar. 2021)
- OpenBSD-2.2/awk.1 (Dec. 1997, OpenBSD seems to have introduced man pages in 2.2)
- NetBSD-2.0/awk.1 (Jun. 2004, NetBSD seems to have switched from gawk to nawk in 2.0)
- NetBSD-1.3/awk.1 (Nov. 1994, gawk)
There was a problem hiding this comment.
- [PDF] AIX 5L 5.3 Commands Reference a-c/p179 mentions
gsubtoo (Aug. 2004)
There was a problem hiding this comment.
Could be that it was Solaris awk indeed. Or some old version of mawk. Anyway I think we're good.
No description provided.