Live data from Hacker News

Safe ways to do things in bash

github.com

201–210 of 255 posts

Re: Safe ways to do things in bash

#201
post #95

Earlier quoted context omitted.

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 y…

> 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...

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

#202

Earlier quoted context omitted.

> 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 :-)

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 backw…

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

#203
Reason number 650 why I love Ruby: it offers ridiculously gradual transitions from shell scripts. One time I built up a pretty weighty conditional-heavy script in Bash and didn't want to keep adding to it. So I simply made all the bash calls use backticks. Took me all of a few minutes to get option parsing working again.

One day I'll learn the Ruby way of doing shell one-liners and that'll hopefully keep me out of man pages for that sort of thing forever.

Re: Safe ways to do things in bash

#204
post #152

Earlier quoted context omitted.

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.

Yes, but which one, and which version? This is especially painful with Python ;)

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

#205
post #95

Earlier quoted context omitted.

> 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...

Nobody said it couldn't be done, just that it shouldn't. Fun stunt but ultimately suitable only for toys or as a gimmick.

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

#206
post #8

From the article: > Should I use curly braces? Bad: some_command $arg1 $arg2 $arg3 Extra bad (cargo culting unnecessary braces): some_command ${arg1} ${arg2} ${arg3} Correct: some_command "${arg1}" "${arg2}" "${arg3}" Better: some_command "$arg1" "$arg2" "$arg3" > In the "extra bad" and "correct" examples, braces compete with quotes under the limits of tolerable verbosity. > Shellharden will rewrite all these variant…

> [1]: "${foo} bar baz" v.s. "$foo"" bar baz"

Why would you quote the second option like that? You can just write: "$foo bar baz"

Re: Safe ways to do things in bash

#207

Earlier quoted context omitted.

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.

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…

Thanks!

Re: Safe ways to do things in bash

#208

Earlier quoted context omitted.

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 backw…

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.

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

#209

Earlier quoted context omitted.

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.

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.

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

#210
post #197
post #149

Earlier quoted context omitted.

I'm all for "command -v", but do you know which OSes don't have "which" by default?

A straw man question. That's not the problem with which. * https://github.com/koalaman/shellcheck/wiki/SC2230 * https://unix.stackexchange.com/questions/85249/

[deleted]
Post reply on HN