103
Bringing the Unix Philosophy to the 21st Century
(blog.kellybrazil.com)
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Follow the wormhole through a path of communities !webdev@programming.dev
The author it trying to solve non-existing problem with the tool that does not meet requirements that he presented himself.
Yeah, it's awful. But wait… Could one achieve this a simpler way? Assume we never heard about
ifconfig
deprecation (how many years ago? 15 or so?). Let's see atifconfig
output on my machine:Seems that the
cut
part of pipeline is not needed because netmask is specified separately. The purpose ofhead
part is likely to avoid printing IPv6 address, but this could be achieved by modifying a regular expression. So we get:If you know a bit more about
awk
than onlyprint
command, you change this toBut now remember that
ifconfig
has been replaced with theip
command (author knows about it, he uses it in the article, but not in this example that must show how weird are "traditional" pipelines). It allows to use format that is easier to parse and that is more predictable. It is also easy to ask it not to print information that we don't need:It has not only the advantage that we don't need to filter out any lines, but also that output format is unlikely to change in future versions of
ip
whileifconfig
output is not so predictable. However we need to split a netmask:The same without
awk
, in plain shell:Is it better than using JSON output and
jq
? It depends. If you need to obtain IP address in unpredictable environment (i. e. in end-user system that you know nothing about), you cannot rely onjq
because it is never installed by default. On your own system or system that you administer the choice is between learningawk
and learningjq
because both are quite complex. If you already know one, just use it.Where is a place for the
jc
tool here? There's no. You don't need to parseifconfig
output,ifconfig
is not even installed by default in most modern Linux distros. Andjc
has nothing common with UNIX philosophy because it is not a simple general purpose tool but an overcomplicated program with hardcoded parsers for texts, formats of which may vary breaking that parsers. Before parsing an output of command that is designed for better readability, you should ask yourself: how can I get the same information in parseable form? You almost always can.