by 0x4FFC8F on 6/4/20, 7:33 PM with 374 comments
by gorgoiler on 6/4/20, 8:53 PM
(1) everything is text
(2) everything (ish) is a file
(3) including pipes and fds
(4) every piece of software is accessible as a file, invoked at the command line
(5) ...with local arguments
(6) ...and persistent globals in the environment
A lot of understanding comes once you know what execve does, though such knowledge is of course not necessary. It just helps.
Unix is seriously uncool with young people at the moment. I intend to turn that around and articles like this offer good material.
by cowmix on 6/4/20, 8:02 PM
by atombender on 6/5/20, 12:48 AM
$ alias fail=exit 1
$ find / | fail | wc -l; echo $?
0
0
You can turn on the "pipefail" option to remedy this: $ set -o pipefail
$ find / | fail | wc -l; echo $?
0
1
Most scripts don't, because the option makes everything much stricter, and requires more error handling.Of course, a lot of scripts also forget to enable the similarly strict "errexit" (-e) and "nounset" options (-u), which are also important in modern scripting.
There's another error that hardly anyone bothers to handle correctly:
x=$(find / | fail | wc -l)
This sets x to "" because the command failed. The only way to test if this succeeded is to check $?, or use an if statement around it: if ! x=$(find / | fail | wc -l); then
echo "Fail!" >&2
exit 1
fi
I don't think I've seen a script ever bother do this.Of course, if you also want the error message from the command. If you want that, you have to start using name pipes or temporary files, with the attendant cleanup. Shell scripting is suddenly much more complicated, and the resulting scripts become much less fun to write.
And that's why shell scripts are so brittle.
by geophile on 6/4/20, 8:04 PM
I'm developing a shell based on these ideas: https://github.com/geophile/marcel.
by ketanmaheshwari on 6/4/20, 8:06 PM
A case in point is this pipeline that I came across in the wild:
TOKEN=$(kubectl describe secret -n kube-system $(kubectl get secrets -n kube-system | grep default | cut -f1 -d ' ') | grep -E '^token' | cut -f2 -d':' | tr -d '\t' | tr -d " ")
In this case, perhaps awk would have absorbed 3 to 4 stages.
by russellbeattie on 6/4/20, 10:14 PM
First I'll use the command line to, say, grab a file from a URL, parse, sort and format it. If I find myself doing the same commands a lot, I'll make a .sh file and pop the commands in there.
But then there's that next step, which is where Bash in particular falls down: Branching and loops or any real logic. I've tried it enough times to know it's not worth it. So at this point, I load up a text editor and write a NodeJS script which does the same thing (Used to be Perl, or Python). If I need more functionality than what's in the standard library, I'll make a folder and do an npm init -y and npm install a few packages for what I need.
This is not as elegant as pipes, but I have more fine grained control over the data, and the end result is a folder I can zip and send to someone else in case they want to use the same script.
There is a way to make a NodeJS script listen to STDIO and act like another Unix utility, but I never do that. Once I'm in a scripting environment, I might as well just put it all in there so it's in one place.
by nojito on 6/4/20, 9:53 PM
This is one clear area where Powershell with its object model has got it right.
by antipaul on 6/5/20, 2:33 AM
Compare the following 2 equivalent snippets. Which one seems more understandable?
iris_data %>%
names() %>%
tolower() %>%
gsub(".", "_", ., fixed=TRUE) %>%
paste0("(", ., ")")
or: paste0("(", gsub(".", "_", tolower(names(iris_data)), fixed=TRUE), ")")
by CalmStorm on 6/4/20, 8:58 PM
Unix pipelines actually helped me make sense of Haskell's monad.
by nsajko on 6/5/20, 1:35 AM
by lexpar on 6/4/20, 9:32 PM
https://github.com/prithugoswami/personal-website/blob/maste...
by rzmnzm on 6/4/20, 8:45 PM
Passing objects through the pipeline and being able to access this data without awk/sed incantations is a blessing for me.
I think anyone who appreciates shell pipelines and python can grok the advantages of the approach taken by Powershell, in a large way it is directly built upon existing an Unix heritage.
I'm not so good at explaining why, but for anyone curious please have a look at the Monad manifesto by Jeffrey Snover
https://devblogs.microsoft.com/powershell/monad-manifesto-th...
You may not agree with the implementation, but the ideas being it, I think, are worth considering.
by akavel on 6/4/20, 11:40 PM
I've also recently seen it being described in shorter words as a "sticky REPL for shell". Hope you like it, and it makes your life easier!
by tekert on 6/5/20, 6:31 AM
by tarkin2 on 6/5/20, 8:46 AM
Pipes that loop, outputting a declarative gui text format, and listen for events from the gui, would be marvellous.
I can’t think how to do that without sockets and a bash loop. And that seems to create the kind of complexity that pipes manage to avoid.
by benjaminoakes on 6/5/20, 11:47 AM
by parliament32 on 6/4/20, 10:21 PM
by theshadowmonkey on 6/15/20, 3:05 AM
by sedatk on 6/4/20, 8:43 PM
by enriquto on 6/4/20, 9:46 PM
file -b `echo $PATH:|sed 's/:/\/* /g'`|cut -d\ -f-2|sort|uniq -c|sort -n
it prints a histogram of the types of all the programs on your path (e.g., whether they are shell, python, perl scripts or executable binaries). How can you ever write such a cute thing in e.g., python or, god forbid, java?by stevefan1999 on 6/5/20, 3:37 AM
by praveen9920 on 6/5/20, 1:33 PM
Looking at this code, I am tempted to reimplement this using pipes but saner mind took over and said "don't fix something that is not broken"
I probably would be still do it and get some benchmark numbers to compare both.
by hi41 on 6/4/20, 9:20 PM
by Silamoth on 6/4/20, 11:32 PM
by miclill on 6/5/20, 9:51 AM
by not2b on 6/4/20, 10:34 PM
by sigjuice on 6/5/20, 1:37 AM
by kazinator on 6/5/20, 10:02 PM
Text filtering approach narrowly rescued by website feature.
Phew, that was close!
by stormdennis on 6/4/20, 9:45 PM
by mehrdadn on 6/4/20, 9:02 PM
by jakubnarebski on 6/9/20, 9:02 PM
by JadeNB on 6/4/20, 7:49 PM
by 0xsnowcrash on 6/5/20, 7:47 AM
by nickthemagicman on 6/4/20, 8:16 PM
by unnouinceput on 6/5/20, 1:25 AM
by hbarka on 6/5/20, 1:13 AM
by Dilu8 on 6/5/20, 8:05 AM
by staycoolboy on 6/4/20, 10:43 PM
If you like pipes and multimedia, checkout gstreamer, it has taken the custom pipeline example to real-time.
by known on 6/5/20, 4:11 AM
by billfor on 6/4/20, 10:10 PM
by niko0221 on 6/4/20, 11:36 PM
by fogetti on 6/4/20, 11:57 PM
But is this really HN top page worthy? I have seen this horse beaten to death for decades now. These kind of articles have been around since the very beginning of the internet.
Am I missing something newsworthy which makes this article different from the hundreds of thousands of similar articles?
by adamnemecek on 6/4/20, 9:57 PM