199 points · by signa11 · 2y ago · 83 comments
You can practice some of the exercises using this interactive TUI app: https://github.com/learnbyexample/TUI-apps/tree/main/CLI-Exe...
See also: https://ratfactor.com/slackware/pkgblog/coreutils, https://maizure.org/projects/decoded-gnu-coreutils/ and https://www.pixelbeat.org/docs/coreutils-testing.html
echo "Hello World!" | xxd
which is not part of coreutils ... but vim [2] confusingly.But also hexdump is not part of coreutils.
They have `od`, which nobody uses because it looks like this:
echo "Hello World!" | od -A x -t x1z -v
[1] https://blog.robertelder.org/intro-to-mkfifo-command/[2] https://github.com/vim/vim/blob/d33afe12c6639d70fca82230df6b...
vi, is something that you must absolutely learn. Learning vi(especially with macros) will give you new perspectives about thinking and working with plain text in ways you might not have imagined before. Most of it might be one time work, but it generally saves you lots of spreadsheet and scripting work. Many times you even realise you were doing lots of pointless work over the years.
Perl is the next progression in this path, and this will give you full code paradigm to do with anything Unix and text. Like so as long your application runs on Unixy operating systems, Perl can do it. You can often ship just one file to the server, be guaranteed it will run with backwards compatibility, it will run fast. You can also develop fast and change at will.
Learning bash gives you means to dump a series a commands into a file and repeating them. But many times what saves you time and effort is learning to use for loop on the command line. This helps you do a lot of bulk work in repetition.
At most you should learn Perl to learn now not to design a language and then you should learn bash to learn why such a large percentage of systems are vulnerable to various injections.
Even Apple had a bash error where they had an OS upgrade that deleted your hard drive if you had a space in the volume name which I'm making the assumption was a bash script because spaces in filename don't generally matter in any sane language.
You can live without it. But you will do a lot of pointless work over long periods of time.
There are a lots of people who are ok with that. Me personally, I attempt to live a life where I can do O(1) or O(as little steps it takes) in anything I touch.
I stubbornly refuse to do work that can be done using a computer.
Perl is only unreadable if you decide to write unreadable code or if you can't do any better. It is just a revelator of who the actual programmer is.
It was iTunes 2 installer pkg for Mac OS X 10.1, but that was indeed the cause: https://forums.whirlpool.net.au/thread/v29rw7z3#r13436
What if you
* only write readable Perl
* don't interact with unreadable Perl
then you can enjoy the benefits of the Perl ecosystem, even tho the language has a lot of shorthandsI have written a rather lot of sh (I target POSIX sh, not bash) and I'm decently familiar with common cli tools (coreutils, moreutils, etc.). IME awk+sed cover most of the "fancy" text manipulation just fine; what am I missing out on if I skip perl?
Think of it like a unix native language. Like the immediate extension to the operating system itself.
Apart from that Perl is very stable, fast, and very deeply committed to backwards compatibility. You can be sure the scripts you wrote years back will run on new Perls.
awk+sed are simple match-do engines, you can't do counting(atleast not easily), you can't do other myriad stuff like working with >2-3 files at one time(iterating simultaneously, in sub-loops etc).
If you are dealing with large files, in Perl its fairly simple to build a index by doing one go on the whole file, then use the key to map to the value where you want to go in the file using seek/tell(with pack/unpack), This makes frequent look ups very computationally cheap. In awk/sed you can't do error handling, you can't do reading and writing to multiple files at the same time et al easily atleast. In Perl, you get dbm drivers and functions install so its relatively straight forward to build look up applications of all kinds.
Perl also comes with OO, so if your code gets lengthier you can can fit it into neat design patterns.
Last but not the least. Perl has CPAN. Its just one of those things. Only real competition to CPAN till date is NPM. Not even Java/Python have a library ecosystem as large as Perl.
If you need something more robust or sophisticated than sh(1)& cie, it's best to head up to a statically typed language instead -- I tend to favor go, in part because of the cross-compilable fat binary thing. The lack of static typing is especially annoying when things are getting beefy enough to consider moving away from sh(1).
For prototyping, if you know Perl already, why not; but even there, I personally reach out to sh(1) & friends instead.
Python it is, these days, for maintainability. one shot quick regex things? sed. anything else python.
and I don't say "switch", btw, if you're proficient I there is no value and no need ditching it.
however if you're new to the CLI game, then I'd _not_ suggest perl but python instead.
Scripting is a lot about text, but its not only about text. You need to neatly integrate with many other OS things. More importantly the language needs to be designed such that such tasks can be done with ease.
Python is more like good a small application programming language. Not that great if you do heavy OS, text or command line interaction work. It feels like a less flexible Java in that regard.
Python is a bit wordy for small things but it become extremely concice and reusable once things get a tiny bit larger.
Generally I find doing things in Python is straightforward whereas using shell is more of a low level puzzle game of conversions and reformats and fragility.
The puzzles in Python are higher level using better data types and containers. But also it's much easier to wrap things up into modules and reuse code in Python. And yeah Python is slow... but not compared to bash (unless you're using things like parallel, but Python isn't necessarily horrible for that either)
That's been my experience anyway.
Naw. As a game and VR programmer I only barely need to acknowledge that POSIX exists. These tools are not even Top 100 in usefulness.
It's probably true that game development is really different from, say, web development, when it comes to the relevance of Unix. But I think it's also true that often, when you don't know common Unix utils, you don't know what you're missing. I don't think any programmer could master them and regret it.
This is not by any means elegant. What happens when there is a line that needs some error checking and handled a little differently? Or just a bad line? Once you realise that error tracing, and fixing is just not possible for non trivial stuff you are better off now skipping the entire ceremony and simply use Perl.
As matter of fact, Larry Wall invented Perl while trying to the very same things you mention with C and awk. Then you realise you are dealing with so many adhoc things, without dependency checks, error handling and then you actually need a fully blown programming language to do this sort of work.
It's a very amusing irony if a Perl enthusiast genuinely believes that AWK can only be used in the form of cryptic one-liners, especially in the context of POSIX tools one "must" learn.
And until IDEs became common in UNIX land, I would rather be on Emacs camp.
For the next steps: Perl.
csplit stand for context split, iirc.
there is also the split command. it splits on the basis of number of lines, which can be specified in various ways.
didn't know about the '--filter' flag earlier. it allows you to filter each output file through some other command. useful.
I gotta reserve a weekend to go through this vast and humoristisk set of knowledge.
Thank you Robert!
Contrived example, for brevity:
awk 'BEGIN { while (i < 10) print i++ }' | paste -sd, -
or maybe: ls *txt | paste -sd, - printf "1\n2\n3" | paste -sd+ | bc
6
I suppose you could do that more cleanly with awk, but this has worked whenever I needed it. I find it easier to type.It's fascinating that there are so many different ways to do things, even within the limited confines of coreutils!
seq 10 | paste -sd, -The manpage is opened with your $PAGER, so you can use your favourite search and navigation commands.
Ideally your system has a `man 1 intro` to guide you to things.
[1] Paths might be different for Nix or MacOS users
C programmers really need to be banned from naming things. Naming things is a certified “hard problem”… but it’s not that hard!
In any case, (a) UNIX (as it was spelled then) antedates C, so it's more that the UNIX spelling conventions influenced C's development; and (b) lots of earlier and later programming languages have had mystical abbreviations. For example, ALGOL68 replaced “integer” with “int”, for reasons I have never understood; Wirth's original CDC 6600 had a data type “alfa”, which was a 10-character string; William Wulf's Bliss language had library routines with names like “getnum” rather than “get_number”. In PL/I, you can write “proc” for procedure, and “dec” for “decimal”.
Me, I prefer to program in a language that has primitive procedure names like “call-with-current-continuation”.
It's helpful to understand the context which evolved this desire for terseness.
A) in the 60s through to the early 90s, to communicate with the "computer" you used a terminal. They used a serial connection which started out life very slow. Sending 7 chars (integer) was measurably longer than 3 (int). Commands used a lot (ls) benefited from being short.
B) CPUs were slow. It's hard to fathom now but not only were the CPUs slow they were shared (in many cases) by multiple terminals. Simply processing extra letters, matching strings to known commands, goes up the longer the command.
C) storage costs would make your eyes water. Source code was terse partly for compiling performance, but also to lower storage cost. "Huge" storage meant "a few mega bytes". Most storage was measured in kilo bytes.
D) same for ram. Storing the word "integer" in ram (in your text editor) took up ram that could be used elsewhere.
Of course none of this applies now. But unfortunately habits learned early are hard to break.
(Of course, you should use mosh instead of ssh with high ping servers when you’re not actively trying to appreciate the terseness.)
DEC systems often had (at least) two options for serial communications, a cheaper PIO-based one and cards that could do DMA (yes, DMA for 300-9600 baud serial lines). The former would cause more CPU load. Yes, minicomputers where so slow that handling a 9600 baud serial line (~900 characters per second at most) wasn't negligible.
This fostered shorter meaningful "names" for everything from types to variables to system commands; things were far easier to grasp when short operations fitted on a single line, at worst spilled onto few others, and functional groupings fitted on a screen.
I love most of the language, but I'm convinced it was written by people who type 300 wpm.
It’s possibly more to do with the imposition of having to send commands with a teleprinter running at 75 baud than a preference for terse commands. Terminals in the very early 70s weren’t much better. Limited to 72x20 characters and not hitting the heady speed of 9600 baud until much later in the 70’s.
Get-Content -Tail 20 -Wait
Nevermind that it has tab-completion of not just the commands but the parameter names too. Nevermind that it deals with piping actual objects around rather than forcing everything to be a string. What really matters to developers hearts is that everything they type is 2 characters or less and can demonstrate their extensive memory for whether it's du or df for checking space on a drive.