3 ms·
It doesn't really make sense to have unix commands output json or yaml. They're already doing a form of parsing internally, you're then asking them to encode th
by shatteredgate 5y ago
It doesn't really make sense to have unix commands output json or yaml. They're already doing a form of parsing internally, you're then asking them to encode the output in another text format so you can parse it again. Why? You'd have better results just using the underlying API to get a list of files, and this is what every other program that needs to get a list of files actually does. From a scripting perspective this is one of the things that powershell actually got right.
- AtlasBarfed 5y agowell, because, ls, lsblk, ps, and a host of other table-based outputs aren't reliably parseable. Their columns overflow. They aren't durable to line breaks in indentifiers and other UTF8 and control character oddities. You know what is? A decent data output format. And JSON is the best of the compromises that exist now. YAML would be nice because if you are outputting json, yaml output is easy and it's a bit more readable. That doesn't violate the UNIX philosophy of text output. It probably improves it. Underlying APIs aren't as universal, portable, or well-known. We are talking basically GNU cross-platform programs.
- shatteredgate 5y agoBut you don't actually want to parse them though, that's the mechanism by which you receive the data but the real end goal is you just want the data in its canonical format so it can used programmatically; in powershell there is no parsing, the output is always the canonical representation. I don't get what you meant in the last sentence, the underlying API is glibc which is also cross platform and is incredibly well known. Other cross-platform languages like Java, Golang, Rust, etc have their own portable APIs to enumerate directories that are cross platform.
- AtlasBarfed 5y agoPerl. Python. Bash or shell scripts. They all have "invoke this shell command" at a minimum. Then you get into ssh and a host of other command invocations to get information. The shell is the convenient way to invoke commands to get the information: locally, remotely, quickly, no setup of C bindings, lots of invocation options. If you are polyglot, do you want to use a command you know (ls) or learn each language's infernal syntax for filtering, recursive listing, etc, or learn it once with shell?
- shatteredgate 5y ago>If you are polyglot, do you want to use a command you know (ls) or learn each language's infernal syntax for filtering, recursive listing, etc, or learn it once with shell? Probably learn the language's syntax because it's faster and more powerful, e.g. in python and perl I'd say don't bother with running shell commands and doing unnecessary forking and parsing if you can avoid it, use the built-in glob instead: https://docs.python.org/3/library/glob.html https://docs.python.org/3/library/glob.html https://perldoc.perl.org/functions/glob https://perldoc.perl.org/functions/glob Most languages just have this now because it's easier and faster than invoking external programs and is a better cross platform solution, glibc even has it: https://man7.org/linux/man-pages/man3/glob.3.html https://man7.org/linux/man-pages/man3/glob.3.html