News for hackers. Stuff that matters.
Re:Safe ways to do things in bash by freedomben on Monday May 14, 2018 @08:34PM (#17070664)
I've written a ridiculous amount of shell script in my day, especially when doing "devops" before we had a term like "devops" to describe it. I've fallen in almost every pit bash has. With that background, here is my opinion.

1. This article contains excellent advice and should be starred for later retrieval.

2. Having basic scripting skills will make you a way better programmer. Many times I've done huge refactors and needle-in-hay-stack searches using only shell commands.

3. Shell is the universal language.

4. Bash isn't that bad once you get used to it (seriously. I'll grant you tho that arrays are still nasty ;-) ).

5. Bash is not that dangerous if you follow best practices. Don't be lazy!

6. You will not regret getting really good at shell script. You'll have to take my word for it now because you don't know what you're missing.

Re:Safe ways to do things in bash by digitalsushi on Monday May 14, 2018 @09:39PM (#17070939)
Re 6: I am an /extremely/ mediocre developer but I have crafted some real 99th percentile bash skills (within my company, not globally) and I completely self-sustain myself on just that. (You're wondering how many people write bash where I am, and the answer is somewhere between 8 and 15 thousand people).

It's funny how many things that seem kind of incredible for a pure bash solution, looking back over the past 21 years doing this drivel. This year is the first I had to learn how to export all of the shell's variables and function definitions for a forked clone to run asynchronous background processing of the main thread. Bash! Bash of all things. Probably 10 seconds in any other language, but here is bash kind of hopping alongside. Callbacks, reflective programming, there's always a really awkward way in bash to do what's happening in the popular languages.

Re:Safe ways to do things in bash by koolba on Monday May 14, 2018 @09:49PM (#17070996)
> It's funny how many things that seem kind of incredible for a pure bash solution, ...

My go to example of “Bash can do whaaat?!” is the source for xip.io. A custom DNS server written in a handful of lines of Bash!

https://github.com/basecamp/xip-pdns/blob/master/bin/xip-pdn...

Re:Safe ways to do things in bash by ddtaylor on Monday May 14, 2018 @10:58PM (#17071304)
I have made a lot of DNS products and doing the same thing happening here is a few lines of code in most any language. This code, while entertaining, cannot handle very simple DNS packets because of its "compression encoding.
Re:Safe ways to do things in bash by kiallmacinnes on Tuesday May 15, 2018 @12:27AM (#17071645)
This is ran behind PowerDNS, which handles the compression prior to handing off to this code, so that shouldn't be a problem.
Re:Safe ways to do things in bash by brazzledazzle on Tuesday May 15, 2018 @04:51AM (#17072563)
Which means it’s not really doing any of the heavy lifting.
Re:Safe ways to do things in bash by xyproto on Tuesday May 15, 2018 @06:51AM (#17073019)
Which is also the case for most bash scripts, the heaving lifting is done by all the executables that are called from bash, not in native bash.
Re:Safe ways to do things in bash by opinionator1 on Tuesday May 15, 2018 @09:17AM (#17073660)
Nobody said it couldn't be done, just that it shouldn't. Fun stunt but ultimately suitable only for toys or as a gimmick.
Re:Safe ways to do things in bash by digitalsushi on Tuesday May 15, 2018 @09:36AM (#17073788)
I've developed on consumer grade routers from big vendors - Asus, linksys, netgear etc, and they are all using identical quality scripts for a large majority of the router's data processing. These scripts may not be pristine, but they are sure not always just a toy.
Re:Safe ways to do things in bash by saltcured on Tuesday May 15, 2018 @11:12AM (#17074506)
Over 20 years ago, I was home on a sick day in the middle of refactoring a build system based on Bourne shell. We could not use any of the new features of BASH but had to stick to what worked as /bin/sh across a bunch of supercomputer vendors. I was finding a lot of arcane tricks to optimize execution on those systems, some of which had improbably expensive fork-exec.

In my fevered state, I wrote some arithmetic functions to do the equivalent of `expr $a + $b` and `expr $a \* $b` without any fork-exec to external commands, just to prove to myself that it could be done. I think I abused things like $IFS, for loops, recursive function calls, and case statements to make an arbitrary-precision decimal adder function.

Re:Safe ways to do things in bash by h1d on Tuesday May 15, 2018 @04:21AM (#17072452)
I think it's time people start using something better than bash/zsh that is decades old, like fish or even come up with a more modern shell.

Even by looking at these examples, you see it has less verbosity like "then" and "do", you can reference arguments as $argv instead of cryptic $@ and exit status code as $status instead of $? which is confusing with $! and the likes.

https://blog.codeship.com/lets-talk-about-shell-scripting/

https://fishshell.com/docs/current/tutorial.html

Shell is such an integral part of admins and programmers workflow yet I find it hard to believe this field has been so slow at improving. Even the fish site jokingly states "Finally, a command line shell for the 90s" implying the others are even older.

Re:Safe ways to do things in bash by Sean1708 on Tuesday May 15, 2018 @06:32AM (#17072937)
You might find the Oil Blog[0] interesting, there are a lot of interesting thoughts there about what a truly modern shell might look like.

[0]: https://www.oilshell.org/blog/

Re:Safe ways to do things in bash by h1d on Tuesday May 15, 2018 @07:23AM (#17073143)
Great read, thanks.
Re:Safe ways to do things in bash by zingmars on Tuesday May 15, 2018 @05:28AM (#17072719)
I think a big issue is that bash is available everywhere, while fish might not be. There's also the fact that a lot of us have fancy dotfiles for our work/home computers, and switching to another shell would mean having to rewrite them in the target shell language.
Re:Safe ways to do things in bash by h1d on Tuesday May 15, 2018 @05:47AM (#17072788)
So, you'd rather use something that is decades old because you can't/don't want to (ask to) install 1 new program on the server and take a weekend to rebuild your config file?

You can simply import aliases as is and in some cases, you may be able to even simplify parts of your config.

Personally, it was easy for me as I'm administering the servers and it was all just installing fish on every servers. (some dozens.)

Re:Safe ways to do things in bash by znpy on Tuesday May 15, 2018 @08:09AM (#17073326)
> So, you'd rather use something that is decades old because you can't/don't want to (ask to) install 1 new program on the server and take a weekend to rebuild your config file?

Absolutely, yes. Because long story short, bash is TriedAndTrue® technology. Stuff that works.

It takes a bit of dedication to be mastered at a decent level but it pays off immensely. It's so ubiquitous it is one of those tools that you can learn once, use for the rest of your life, and use in a lot of contexts.

It's so widespread that it can bring you very far with very little.

All this being said, as someone who used to write code for a living (and now works as a System Engineer, using the bash shell everyday) I must say that if you do not do input verification (according to the language of your choice) and something goes wrong then it's your fault.

Re:Safe ways to do things in bash by opinionator1 on Tuesday May 15, 2018 @09:12AM (#17073637)
"works"

More like "better the devil you know" (if you can even say that much).

Bash doesn't scale. Every shop larger than 1 has been burned by bash gotchas. Use a real scripting language and shell out to the commands and builtins when necessary.

At my shop bash is strictly disallowed in production environments and we're all better off for it.

Re:Safe ways to do things in bash by znpy on Tuesday May 15, 2018 @11:19AM (#17074580)
> Bash doesn't scale.

Thanks mate, I had a good laugh.

I wouldn't expect bash to scale anyway. That's not what it's meant for. It's meant for system administration task automation.

On a more serious note ...

In many occasions, the performance you get depends on how you tackle the problem you have, though. Even using bash and the tools from the unix toolbox, sometimes you can gain significant improvements on how you manage your data.

Anecdotal: I cannot remember the details, but I remember that rearranging the order of sorting and searching and removing duplicates (sort, sort -u, grep, uniq mainly) I saw a significant speedup.

Anecdotal (2): I cut the execution time of a night-running job from hours to minutes (tens of minutes, to be honest - but still less than an hour) just by slicing the size of a problem into smaller parts and by handling each slice in parallel (the machine had 48 cpus, but the problem was being solved "sequentially" on one cpu alone). I wrote some 30-50 lines of python, just to implement parallelism control: the rest of the problem was still handled with bash script. Partial results were reassembled at the end. Bash has coprocesses, so I might have handled that in bash as well, but python was more handy at the time (meh, i just wanted to optimize that problem).

What I am trying to say is that sometimes the "scaling" you get is justified by the size of the problem, sometimes it's not.

Re:Safe ways to do things in bash by znpy on Tuesday May 15, 2018 @11:26AM (#17074635)
It's worth noting that bash is really a glue language to call other programs. If you mainly use the tools from the unix toolbox (i'm thinking of grep, for example) you really get the "scaling" (the performance) of native executable code.

Again, it really depends on how you handle your data.

Having a number of filters chained via pipes is really efficient, for example, when compared with looping over an array and executing some python/perl/ruby one-liners every time.

Re:Safe ways to do things in bash by opinionator1 on Wednesday May 16, 2018 @12:47PM (#17083925)
I should have clarified. I'm not talking about micro-optimizations. Bash doesn't scale operationally. You might be a bash wizard who never fucks up, but you're never going to be able to keep non-wizards from having to use your awful bash codebase.

Bash is not at all meant for "system administration task automation", the very idea is ludicrous. It has to be the most singularly ill-purposed invention ever to be applied towards task automation.

Re:Safe ways to do things in bash by znpy on Tuesday May 15, 2018 @11:37AM (#17074745)
> Use a real scripting language and shell out to the commands and builtins when necessary.

Not to mention, for example: https://julialang.org/blog/2012/03/shelling-out-sucks

Re:Safe ways to do things in bash by andrewshadura on Wednesday May 16, 2018 @02:02AM (#17080319)
It is not webscale!
Re:Safe ways to do things in bash by robot01 on Tuesday May 15, 2018 @11:31AM (#17074696)
Do you guys have a different, approved shell for prod?
Re:Safe ways to do things in bash by opinionator1 on Wednesday May 16, 2018 @12:44PM (#17083882)
Nobody should be using any shell in prod
Re:Safe ways to do things in bash by squeaky-clean on Tuesday May 15, 2018 @07:49AM (#17073256)
It's not just me, but convincing my team to switch to a new scripting language. Especially if they've never heard of fish and have been using bash/sh their whole careers.
Re:Safe ways to do things in bash by yani on Tuesday May 15, 2018 @05:33AM (#17072735)
Availability is a big issue. It is nearly impossible to get a seasoned sysadmin to install fish for you when bash is available.
Re:Safe ways to do things in bash by h1d on Tuesday May 15, 2018 @05:49AM (#17072796)
If you're convinced your productivity may change by ditching bash, I'm not sure what kind of counterargument the sysadmin will bring.
Re:Safe ways to do things in bash by petee on Tuesday May 15, 2018 @07:39AM (#17073203)
Avoiding the next Shellshock? As bad as it was, who knows what goodies an even less-audited shell has in store
Re:Safe ways to do things in bash by h1d on Tuesday May 15, 2018 @10:52AM (#17074337)
Using bash never prevented the problem in the first place.
Re:Safe ways to do things in bash by petee on Tuesday May 15, 2018 @12:24PM (#17075138)
Re:Safe ways to do things in bash by wheaties on Tuesday May 15, 2018 @12:45PM (#17075310)
What's wrong with something that's decades old? Bash is great for command line and small scripts. It's once you surpass 100 lines where things get problematic. At that point, you're not writing a shell script, you're writing a tiny application and need to treat it as such.
Re:Safe ways to do things in bash by Silhouette on Tuesday May 15, 2018 @01:38PM (#17075867)
What's wrong with something that's decades old?

In the case of shells, not benefitting from the extra decades of experience. There are some horrible things that you can do with Bash just by mistyping a single character or using the wrong type of quotes or failing to appreciate how something will expand in some edge case. This is not a desirable property for an environment that people use after being paged at 4am, while their employer is losing $XXX,000/minute because something critical is down, and where the half-asleep operator is one short command away from deleting the universe without so much as a confirmation prompt.

Re:Safe ways to do things in bash by freddref on Tuesday May 15, 2018 @07:03AM (#17073061)
I've started using Ammonite for scripting, which certainly feels more solid than bash.

https://github.com/lihaoyi/Ammonite

Many times previously I've starting really quickly testing/prototyping an idea in bash, which eventually grew large and unweildy. Ammonite appears like it might allow quick hacking, but also the option to easily transition your scripts into scala proper while leveraging the good bits of Java.

Re:Safe ways to do things in bash by stronglikedan on Tuesday May 15, 2018 @11:12AM (#17074508)
Why not both?

> Bash has arrays and a safe mode, which may make it just about acceptable under safe coding practices, when used correctly.

> Fish is easier to use correctly, but lacks a safe mode.

> Prototyping in fish is therefore a good idea, provided that you know how to translate correctly from fish to bash.

Re:Safe ways to do things in bash by collinmanderson on Tuesday May 15, 2018 @07:30PM (#17078779)
Doesn’t the original article say fish isn’t safe?

> Fish is easier to use correctly, but lacks a safe mode. Prototyping in fish is therefore a good idea, provided that you know how to translate correctly from fish to bash.

Re:Safe ways to do things in bash by yAnonymous on Tuesday May 15, 2018 @06:12AM (#17072878)
Just make the start. When a modern shell becomes reliable and popular enough, it'll slowly replace the old ones.
Re:Safe ways to do things in bash by Groxx on Tuesday May 15, 2018 @02:19AM (#17072016)
Re 4: bash is an acceptably-good domain-specific language.

Which leads right into 5: that domain is unsafe-by-design and failure-tolerant. Which is phenomenally useful and efficient for one-off tasks, and phenomenally dangerous for something you allow others to inject behavior into.[1]

There are a fair number of things which bash could do better, and probably should. But many of the gotchas like "if you don't quote it, it'll expand into multiple arguments" are features when you want to throw things around efficiently. It wouldn't be as efficient for plugging so many disparate tools together if it weren't such an efficient footgun.

[1]: If you need anything even remotely fault-resistant, `set -euo pipefail` and use `shellcheck` (and the rest of this resource, it is indeed great) and then consider using a "normal" programming language instead. But for behavior you fully control it's not too bad if you're careful.

Re:Safe ways to do things in bash by themodelplumber on Monday May 14, 2018 @10:41PM (#17071219)
> 6. You will not regret getting really good at shell script. You'll have to take my word for it now because you don't know what you're missing.

Stories! C'mon, it was a long Monday :-)

Re:Safe ways to do things in bash by freedomben on Monday May 14, 2018 @11:50PM (#17071520)
Haha. Mostly it is dozens, probably hundreds of little things over time when turning to the shell proved highly productive and effective. The shell is surprisingly really, really good at processing text (using the good 'ol unix tools), and it's amazing how often "text processing" type problems come up. Whether you are doing find/replace on source code, looking for where certain strings are used/defined, or curling some page and extracting data out of it, the shell can often provide very quickly and effectively. The bonus feature is that people are often very impressed when you whip out the shell skills and nail something they thought would be difficult.

I also started capturing most tasks as bash functions or aliases in my `~/.bashrc` file. This doubles as both a handy reference for me to look at later to remember how to do something, and automating simple things.

Re:Safe ways to do things in bash by themodelplumber on Tuesday May 15, 2018 @12:44AM (#17071703)
That's really cool, thanks. It seems that whenever I come across an annoying text-processing problem, it's always really hard or too generic-sounding to get search engine help with potential bash solutions. However I'd somehow like to improve in that area.
Re:Safe ways to do things in bash by wool_gather on Tuesday May 15, 2018 @08:39AM (#17073474)
Check out the sed and awk tutorials here: http://www.grymoire.com/Unix/Sed.html

sed in particular is really amazing in what it can do, very simply. (Note, depending on your system, you might want to get GNU sed, for "extended" regex support.)

Those pages do a great job breaking down the basic behavior and explaining pitfalls. They're skim-able, and pretty easily searchable after than, so you can come back and remind yourself of the thing you learned last week.

Re:Safe ways to do things in bash by themodelplumber on Tuesday May 15, 2018 @10:11AM (#17074021)
Thanks!
Re:Safe ways to do things in bash by laumars on Tuesday May 15, 2018 @02:18AM (#17072013)
Back before DevOps was a thing, us sysadmins were building the same automation tools in shell scripts. Using rsync, git (cli) and such like instead of docker, GitHub plugins, concourse / AWS Code Deploy etc. And ssh keys on VMware / xen stacks instead of puppet / terraform etc.

As much as I think some of the new generation of tools are pretty awesome for getting stuff done, some of it honestly feels like a step backwards because you're relying on 3rd party services or require a new server(s) to be provisioned to host your management servers; some of which require extra man power just to keep running. And as much as Bash gets a bad rep for hidden traps, I'd take that any day over Terraforms "I'm not really a programming language but I like to pretend to be" markup, HCL.

But that's the nature of the game now; it's less about hacking stuff together and more about using preformed Lego bricks to build the same approximation.

Re:Safe ways to do things in bash by opinionator1 on Tuesday May 15, 2018 @09:20AM (#17073673)
You need to fix your mentality. HCL not being a complete language is a feature. Bash is a liability. If you honestly think you can build more reliable modern systems in bash, you deserve to be laughed right back into the 70s where you belong.
Re:Safe ways to do things in bash by reacharavindh on Tuesday May 15, 2018 @10:24AM (#17074102)
Wow. It's a bit ironic to comment about fixing mentality while you're blatantly ignoring a rational argument with nothing.

I concur with the same statement. Newer domain specific syntaxes and tools are increasing the dependencies and cognitive load to get things done. One would have to remember the yaml syntax, and version specific keywords for Kubernetes, Docker, Ansible and everything in the container world just to deploy a statically linked Go binary. That's where we've come to now. It is worth pondering whether we are increasingly moving towards "give me the lego bricks, and I don't care to know what it specifically does" attitude, and if it's good or bad.

Re:Safe ways to do things in bash by laumars on Tuesday May 15, 2018 @12:05PM (#17074966)
I rarely bother responding to troll accounts but I do feel the need to point a couple of things out:

1/ HCL not being a complete language is a burden when you're writing build scripts that need to target multiple environments (eg dev, UAT, staging, live) as it means you have to use the `count` syntax to include/exclude resources. Also I've frequently run into issues where output will expect a variable or resource to exist even when it's inside a ternary operator where the condition isn't met (isn't the point of an `if` condition that you don't evaluate all blocks of code in the structure?).

2/ The reason HCL isn't a "complete language" isn't because "complete languages" are a liability (clearly that's nonsense); it's because Hashicorp wanted something that met the middle ground between JSON and scripting so that all ranges of technical expertese were comfortable; and they wanted a format that could still be compiled back down to JSON. Personally I feel the monster they created is the worst of both worlds but I do think the goal they were trying to achieve is an honorable one.

3/ I was never trying to make the point that I've built more reliable systems in Bash. However once you start writing pretty complex Terraform projects (some of my build scripts are a several thousand lines of code; inc modules) you really do start to bang your head with Terraform - not all of it because of HCL though. Don't get me wrong, Terraform is definitely the best tool we have at the moment for deploying to "the cloud"; but that doesn't mean it isn't still bloody annoying to work with at times. A few bugs I've run into in the last month:

3a/ Error message wasn't being passed from AWS to the user so infra would fail to build but without explanation. A few hours of debugging and we discovered what resource was failing and why. This was a bug in Terraform so issue raised on Github.

3b/ Workspace name pushed the resource name tag over it's character limit. We did get an error message here but it was inaccurate. Thankfully this is an issue we've run into before so it was pretty quick to resolve.

3c/ The aforementioned problem of all code getting evaluated inside both conditions of a ternary operator

3d/ If your AWS token expires before the Terraform apply is complete you're left with incomplete infrastructure and no valid state file to rollback. This is particularly annoying if your builds do certificate management or ELK; both of which can take 15 to 20 minutes just on those resources alone.

3e/ You don't get line numbers nor even file names when errors are raised which makes debugging through large projects painful.

4/ I'm not about to sing the praises of Bash as a modern deployment tool as it can be a complete nightmare to work with if you're not proficient in it's hidden traps. But to answer your question; yes I have built reliable systems in Bash. It's a tool that has been around for decades, people can and have built some pretty reliable stuff in it and I personally consider myself a veteran. (for what it's worth I have nearly 3 decades of dev skills and have used a lot of other languages we might now consider "dangerous" - some of which are still used in production). As an aside note; it's the decades of years of experience in Bash and other, much lower level, languages that inspired me to write my own $SHELL and scripting language. So I tend to use that more than Bash these days.

5/ I didn't proof read this so apologies if some parts didn't make any sense. It's a long post and in replying to you I'm interrupting fixing one of those massive Terraform projects I described earlier.

Re:Safe ways to do things in bash by emmelaich on Tuesday May 15, 2018 @03:41AM (#17072294)
I probably have a similar history but I've come to the belief that you should not use Bash if you're doing anything fancy or long (>10 lines).

Just use Perl, Python or Ruby. One or all of them is installed on every machine you are likely to use.

Re:Safe ways to do things in bash by davedx on Tuesday May 15, 2018 @04:14AM (#17072412)
Yes, but which one, and which version? This is especially painful with Python ;)
Re:Safe ways to do things in bash by vinceguidry on Tuesday May 15, 2018 @09:34AM (#17073773)
Use Ruby. It doesn't change much from version to version, thank God. I curse python every time I have to install a package. Nothing Just Works.

If you install just one gem, bundler, you can get dependency management in a one-file script. Checkout Bundler Inline:

https://github.com/bundler/bundler/blob/master/lib/bundler/i...

Re:Safe ways to do things in bash by h1d on Tuesday May 15, 2018 @04:28AM (#17072482)
What's painful about Python but not so for others? Writing anything complicated in bash/zsh is painful enough I wouldn't even think about it.
Re:Safe ways to do things in bash by Groxx on Tuesday May 15, 2018 @04:37PM (#17077659)
2 vs 3 is not a small change. and tools for helping you do things (like a 3+2 compatibility lib) get installed with pip... which is horrifyingly fragile. `pip install your-lib` -> you have no idea if you have the dependencies you require, because pip doesn't behave rationally. it often Just Works™, but when it doesn't there's no help and a massive minefield of potential problems.
Re:Safe ways to do things in bash by CJefferson on Tuesday May 15, 2018 @07:46AM (#17073243)
I currently find some users have python2, some have python3, some both. It's fine in a controlled environment, or if your users are Devs, but a pain with normal users who I just want to send a script to.
Re:Safe ways to do things in bash by mncolinlee on Tuesday May 15, 2018 @12:32PM (#17075203)
KotlinScript is yet another option that handles that worry nicely.

https://github.com/holgerbrandl/kscript

Re:Safe ways to do things in bash by yAnonymous on Tuesday May 15, 2018 @06:36AM (#17072954)
Is there an easy way to run shell commands in Python, like backticks in PERL, Ruby or even PHP?

I'd really like to use Python more often, but most of the time it only complicates things with those verbose syscalls.

Re:Safe ways to do things in bash by passwd on Tuesday May 15, 2018 @10:38AM (#17074215)
IPython. Seems to work for simple stuff like `ls blah`, but I'm not aware how far this stretches, and is probably not super safe to use.

EDIT: Seems I've only tried too basic things - it works thanks to %automagic, and seemingly %man is a thing. Other option is to use %%sh or %%bash, but that's a bit verbose.

Re:Safe ways to do things in bash by monort on Tuesday May 15, 2018 @07:08AM (#17073085)
Check out python sh: https://github.com/amoffat/sh
Re:Safe ways to do things in bash by yAnonymous on Tuesday May 15, 2018 @09:02AM (#17073585)
>sh.ls("-l", "/tmp", color="never")

Nope. I really need to be able to test commands on the terminal and then copy/paste them without any modifications. I won't even try to get used to that stupid syntax.

Re:Safe ways to do things in bash by neillyons on Tuesday May 15, 2018 @06:57AM (#17073041)
Unfortunately not. I have used Ruby a little bit and that is really nice feature.
Re:Safe ways to do things in bash by rcxdude on Tuesday May 15, 2018 @11:42AM (#17074807)
part of the complication is due to actually having the proper data seperation that you need to jump through hoops to get in bash. If you go with backtick style calls you lose a lot of that.
Re:Safe ways to do things in bash by sxv on Tuesday May 15, 2018 @02:08PM (#17076182)
Re:Safe ways to do things in bash by freedomben on Tuesday May 15, 2018 @05:38PM (#17078095)
Except for perl, python and ruby are rarely on container images and AMIs I interact with unless they are needed for the application itself to minimize attack surfaces and vectors. Perl is installed sometimes due to dependent packages that themselves depend on perl, but that's becoming much less true as time goes on. With Docker it's also very easy to keep your image super minimal. Most of the time we don't even have bash installed (just sh).
Re:Safe ways to do things in bash by zwischenzug on Tuesday May 15, 2018 @12:26AM (#17071640)
I did a talk on this a while ago:

https://www.youtube.com/watch?v=pb3k0sGKrjQ&t=457s

'Take bash seriously'

Re:Safe ways to do things in bash by h1d on Tuesday May 15, 2018 @04:30AM (#17072492)
It's time we need a better shell than bash instead of thinking it's great after bleeding with it for years and gets used to it.
Re:Safe ways to do things in bash by JdeBP on Tuesday May 15, 2018 @12:10PM (#17075012)
We have just had the 30th anniversary of someone acting upon that very thought.

> Perl is a interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). It combines (in the author's opinion, anyway) some of the best features of C, sed, awk, and sh, so people familiar with those languages should have little difficulty with it. (Language historians will also note some vestiges of csh, Pascal, and even BASIC-PLUS.) Expression syntax corresponds quite closely to C expression syntax. If you have a problem that would ordinarily use sed or awk or sh, but it exceeds their capabilities or must run a little faster, and you don't want to write the silly thing in C, then perl may be for you. There are also translators to turn your sed and awk scripts into perl scripts.

-- Larry Wall, perl 1.0 release announcement, comp.sources.unix v13i001, 1988-01-02.

Re:Safe ways to do things in bash by brazzledazzle on Tuesday May 15, 2018 @05:00AM (#17072613)
I’d vote for PowerShell in a heartbeat. It passes real objects instead of strings. It’s cross platform and open source. It’s imperative but borrows some functional concepts.
Re:Safe ways to do things in bash by agumonkey on Tuesday May 15, 2018 @06:23AM (#17072905)
PowerShell is good. I don't like some of the hardened conventions .. but it's highly convenient without being a mess. Nice output, regular sort/filter functions. I like sed but it goes old quickly.
Re:Safe ways to do things in bash by freedomben on Tuesday May 15, 2018 @05:45PM (#17078136)
I really wonder if PowerShell wouldn't have been much bigger had it been cross-platform from the start. Definitely better late than never (:clap: Microsoft) but at least in my sphere we never even thought seriously about PowerShell because we had too many linux machines in our environment.
Re:Safe ways to do things in bash by zwischenzug on Tuesday May 15, 2018 @02:52PM (#17076613)
As I say in the talk (and get a big laugh) 'Powershell is boring. It's too well-designed.'
Re:Safe ways to do things in bash by zwischenzug on Tuesday May 15, 2018 @02:55PM (#17076640)
The title 'bash is awesome' was explicitly _not_ what I wanted it to be called. If you watch the talk you'll see what I meant.
Re:Safe ways to do things in bash by freedomben on Tuesday May 15, 2018 @12:12AM (#17071591)
Can't edit original, but to answer the question regarding good sources for learning, I highly recommend The Linux Command Line . Great book: http://linuxcommand.org/tlcl.php
Re:Safe ways to do things in bash by antris on Monday May 14, 2018 @10:44PM (#17071229)
What is your opinion on using other languages to augment bash? Awk, perl etc.
Re:Safe ways to do things in bash by freedomben on Monday May 14, 2018 @11:45PM (#17071496)
I personally consider awk, sed, grep, cut, etc. to be "part" of bash. I don't think I ever write a script without invoking those venerable tools at least once. I often have many pipes, such as

  some_command \
    | grep -E 'some.*regex$' \
    | sed -e 's/erase_text//g' \
    | sed -e 's/erase_more//g' \
    | awk '{ print $2 }' \
    | cut -d ':' -f 1
I used to reach for Perl one-liners a lot, and still do sometimes when I need a gross regex that `sed -E` can't handle (See Perl Pie[1]), but Ruby has been making an appearance more and more often for handy one-liners. Roblog has a great post on using Ruby [2].

If the script is complex enough and I'm willing to take a dependency on Ruby, that is usually where I turn. Where that line is does change from time to time. Ruby makes it super trivial to call shell commands and process them easily as well, which makes it easy to integrate into existing scripting [3].

Hopefully that answered your question :-)

[1] http://technosophos.com/2009/05/21/perl-pie-if-you-only-lear...

[2] https://robm.me.uk/ruby/2013/11/20/ruby-enp.html

[3] https://stackoverflow.com/a/2400/2062384

Re:Safe ways to do things in bash by laumars on Tuesday May 15, 2018 @04:08AM (#17072386)
You might already be aware of this, but you can chain multiple sed functions together into one command. eg

    sed -e 's/erase_text//g; s/erase_more//g'
But I appreciate your example there is more for illustrative purposes rather than an actual pipeline you have in production.
Re:Safe ways to do things in bash by zippitydoodah68 on Tuesday May 15, 2018 @04:03PM (#17077359)
These types of pipelines are why bash and the traditional tools are so maligned. Expert knowledge of sed|(g)awk|bash tools is necessary otherwise you end up with companies telling you that you can't use them.

some_command | gawk ' /some.*regex$/ {gsub(erase_text,"");gsub(erase_more,"");split($2,a,":"); print (length(a[1]) ? a[1] : "STRING ERROR")}'

Re:Safe ways to do things in bash by freedomben on Tuesday May 15, 2018 @05:48PM (#17078164)
I'll definitely agree that it can get out of hand. If it's a script/tool that will have many eyes on it and people that need to understand it, bash tools probably aren't the best way to go. I usually turn to ruby in those cases. But even for one off commands, I find myself using pipes like that all the time and I'm the only one that will ever see it, so if it's greek it's no problem :-)
Re:Safe ways to do things in bash by rdtsc on Tuesday May 15, 2018 @01:52AM (#17071928)
> sed -e 's/erase_text//g' \

I like the pattern. Copied it to my notes to try next time I am grepping through logs.

I usually just string a few 'grep -v ignore_this' with pipes but then it ignores the whole line. But I can see how erasing some parts of the line would be very helpful sometimes.

Re:Safe ways to do things in bash by sa46 on Wednesday May 16, 2018 @03:22AM (#17080601)
Drive by code review, you can end a line with the pipe (also && and ||) to avoid a backslash.

    some_command |
      grep -E 'some.*regex$' |
      sed -e 's/erase_text//g' |
      sed -e 's/erase_more//g' |
      awk '{ print $2 }' |
      cut -d ':' -f 1
Re:Safe ways to do things in bash by raboukhalil on Tuesday May 15, 2018 @08:44PM (#17079130)
>> I'll grant you tho that arrays are still nasty ;-)

If you're interested, here's an article I wrote recently that attempts to explain the method in the madness: https://medium.com/p/the-weird-wondrous-world-of-bash-arrays...