107
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
ifconfigdeprecation (how many years ago? 15 or so?). Let's see atifconfigoutput on my machine:Seems that the
cutpart of pipeline is not needed because netmask is specified separately. The purpose ofheadpart 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
awkthan onlyprintcommand, you change this toBut now remember that
ifconfighas been replaced with theipcommand (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
ipwhileifconfigoutput 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 onjqbecause it is never installed by default. On your own system or system that you administer the choice is between learningawkand learningjqbecause both are quite complex. If you already know one, just use it.Where is a place for the
jctool here? There's no. You don't need to parseifconfigoutput,ifconfigis not even installed by default in most modern Linux distros. Andjchas 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.