Comment on More believable for a Linux OS
okamiueru@lemmy.world 10 months agoI don’t think the Unix philosophy of having lots of small tools that do one thing and do it well that you compose together has ever been achieved
Why do you think this might be the case? It’s not remotely accurate, which suggests that you must understand it very differently than I do. To some extent, I am curious.
I’ll give you a recent example. Which is just from yesterday. I had a use case where some program had a memory leak, which would eventually lead to the system running out. So, I “built a program that would monitor this and kill the process that used the most memory”. I don’t know how complicated this is in windows and PS, but it took about 2 minutes in Linux, and it very much leverages the Unix philosophy.
Looks something like this:
get_current_available_memory_mb() { cat /proc/meminfo | grep MemAvailable | grep -oP '\d*' | xargs printf "%d / 1024 \n" | bc }
Each of those pipes do their thing well. The PID of the process using the most memory you can get with something like:
ps aux --sort=-%mem | head -n2 | tail -n1 | awk '{print $2}
Again, using the same Unix philosophy. You then check if the available memory is below some threshold, and send a kill signal to the process if it does. Instead of adding an infinite loop in the script, you stop at making it do that one thing. That is, 1. check remaining memory. 2. if lower than X, kill PID". Let’s call this “foo.sh”. So you end up with is just:
watch -n 2 – ./foo.sh
, and there you go. Every two seconds, it checks available free memory, and saves my system from freezing up.
If memory serves me correctly, PS also supports piping, so I would assume you could do similar things. Would be weird not to, given how powerful it is.
AnyOldName3@lemmy.world 10 months ago
I think you’ve misunderstood my complaint. I know how you go about composing things in a Unix shell. Within your post, you’ve mentioned several distinct languages:
grep -P
ps
’s format expressionsThat’s quite a lot of languages for such a simple task, and there’s nothing forcing any consistency between them. Indeed, awk specifically avoids being like sh because it wants to be good at the things you use awk for. I don’t personally consider something to be doing its job well if it’s going to be wildly different from the things it’s supposed to be used with, though (which is where the disagreement comes from - the people designing Unix thought of it as a benefit). It’s important to remember that the people designing Unix were very clever and were designing it for other very clever people, but also under conditions where if they hit a confusing
awk
script, they could just yell Brian, and have the inventor ofawk
walk over to their desk and explain it. On the other hand, it’s a lot of stuff for a regular person to have in their head at once, and it’s not particularly easy to discover or learn about in the first place, especially if you’re just reading a script someone else has written that uses utilities you’ve not encountered before. If a general-purpose programming language had completely different conventions in different parts of its standard library, it’d be rightly criticised for it, and the Unix shell experience isn’t a completely un-analogous entity.So, I wouldn’t consider the various tools you used that don’t behave like the other tools you used to be doing their job well, as I’d say that’s a reasonable requirement for something to be doing its job well.
On the other hand, PowerShell can do all of this without needing to call into any external tools while using a single language designed to be consistent with itself. You’ve actually managed to land on what I’d consider a pretty bad case for PowerShell as instead of using an obvious command like
Get-ComputerInfo
, you need:(Get-WmiObject Win32_ComputerSystem).FreePhysicalMemory / 1024
Even so, you can tell at a glance that it’s getting the computer system, accessing it’s free physical memory, and dividing the number by 1024.
To get the process ID with the largest working set, you’d use something like
I’m assuming either your
ps
is different to mine, or you’ve got a typo, as mine gives the parent process ID as the second column, not the process’ own ID, which is a good demonstration of the benefits of structured data in a shell - you don’t need sed/awk/grep incantations to extract the data you need, and don’t need to learn the right output flag for each program to get JSON output and pipe it tojq
.There’s not a PowerShell builtin that does the same job as
watch
, but it’s not a standard POSIX tool, so I’m not going to consider it cheating if I don’t bother implementing it for this post.So overall, there’s still the same concept of composing something to do a specific task out of parts, and the way you need to think about it isn’t wildly different, but:
Select-String
does what it says on the tin.grep
only does what it says on the tin if you already know it’s global regular expression print.Specifically regarding the Unix philosophy, it’s really just the first two bullet points that are relevant - a different definition of thing is used, and consistency is a part of doing a job well.