TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Shell-ish scripting in Go with ease

140 pointsby begoon4 months ago

12 comments

0xbadcafebee4 months ago
<p><pre><code> Why shouldn&#x27;t it be as easy to write system administration programs in Go as it is in a typical shell? </code></pre> 1. Shell scripting offers infinite functionality. You can shell script with any program in any language. All it needs to do is take input and produce output. And if the functionality doesn&#x27;t exist, you can create it on the fly, without having to follow any of the traditional rules of programming.<p>2. Shell scripting is a combination of a grammar, operators, a few simple functions, and an extremely loose coupling with generic i&#x2F;o and logic. I don&#x27;t know Go well, but it probably doesn&#x27;t support a similar flexibility. (most languages are very proscriptive about <i>how</i> you can use the language, so you usually can&#x27;t make things as easy as they are in a different, more tailored language&#x2F;interface&#x2F;paradigm. this is why we have DSLs)<p>3. Programmers don&#x27;t really understand the concept of productivity [outside of programming itself]. A programmer would solve a problem by taking 6 weeks to design a perfect program to do the thing. A Sysadmin would take 5 minutes with a shitty language and a shitty tool and get way more done in less time. And re-writing everything into a Go library would always be slower than shell scripting, because it requires re-implementing what a shell script would just use as-is.<p>Scripting is duct-taping the wheel rather than reinventing it. If you want to save yourself a whole lot of time and trouble, just use the duct tape.<p>(also: don&#x27;t go templates exist? why isn&#x27;t that used for scripting)
评论 #42892829 未加载
评论 #42892424 未加载
评论 #42893457 未加载
评论 #42896592 未加载
评论 #42894920 未加载
评论 #42892969 未加载
评论 #42923301 未加载
decasia4 months ago
I just rewrote a tangled 500 line shell script in go.<p>It was my first time writing a golang project at work, so I&#x27;m sure it could have been better. But writing it the naive way, with all the required golang error handling, it ended up taking about 10x more lines of code in golang than the original bash script.<p>It does have a dramatically better UX (largely thanks to spf13&#x27;s cobra and viper), and is way faster than the original, and the codebase is a lot cleaner and more maintainable. So I think it was worthwhile for the users and maintainers.<p>But still, 10x more lines of code. I like the OP, but I&#x27;m still not sure I would reach for golang for short shell scripts.
评论 #42892146 未加载
评论 #42892397 未加载
评论 #42893057 未加载
评论 #42898853 未加载
desumeku4 months ago
This site is horrible. Every comment here is trying their hardest to systematically dismantle this library out of existence through the use of nihilistic mind-games about what &quot;shell scripting&quot; &quot;really&quot; &quot;is&quot;. All because they don&#x27;t like a programming language that much.
评论 #42896804 未加载
breadchris4 months ago
pairing this with yaegi [1] would be interesting. You could having a REPL open doing os operations and when you get the data looking like you want, you select which lines to save to a file.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;traefik&#x2F;yaegi">https:&#x2F;&#x2F;github.com&#x2F;traefik&#x2F;yaegi</a>
评论 #42893925 未加载
评论 #42890454 未加载
emmelaich4 months ago
Too much syntax for a scripting language IMHO.
puika4 months ago
If anyone wants to experiment with this lib + yaegi interpreter I put up a trivial example at [1]. Composing scripts with LSP support and such might be doable with a proper abstraction, in the example a main package with a main function is required. Interpreting might break some functionality for `script` so perhaps rerunning their test suite with yaegi is a good idea if you get serious about this.<p>1. <a href="https:&#x2F;&#x2F;github.com&#x2F;danicc097&#x2F;yaegi-script">https:&#x2F;&#x2F;github.com&#x2F;danicc097&#x2F;yaegi-script</a>
chrooted-user4 months ago
This is awesome! I enjoy writing Go and like all the tooling around it. Think shell scripts are hard to read and was already planning to adjust some shell-like-Go-tool this weekend, so this post has perfect timing :-)
calmbonsai4 months ago
I&#x27;m not going to knock the usefulness of the library, but I am going to knock its application.<p>Architecturally, shell scripts should _exclusively_ be for bootstraps, configs, or extremely localized (individual developer) automation.<p>The New York Minute you need non-trivial error-handling&#x2F;flow-control it&#x27;s no longer a &quot;shell script&quot; and deserves a proper rewrite in a proper programming language.<p>Ian Malcom&#x27;s quote from Jurassic Park comes to mind:<p>&quot;Your scientists were so preoccupied with whether or not they could, they didn&#x27;t stop to think if they should.&quot;
评论 #42892480 未加载
tonymet4 months ago
a killer feature would be adding a dep tree like make and using goroutines to process the tree concurrently .
pyuser5834 months ago
Plenty of languages have a framework to assist with shell&#x2F;Bash scripting. Nothing wrong with that.<p>If you look at the Python build-in API, it&#x27;s pretty terrible. In all fairness, Python strives to work equally well on Windows, Linux, and some other things. Good luck with that.<p>I prefer doing systems scripting in Python over Bash. But writing converting a Bash script to Python without &quot;Pythonizing&quot; it is a bad time.
LinuxAmbulance4 months ago
Very nice, bookmarking this!
synergy204 months ago
for larger scripts,just use python or lua. otherwise,bash is perfect