Live data from Hacker News

Safe ways to do things in bash

github.com

181–190 of 255 posts

Re: Safe ways to do things in bash

#181

Earlier quoted context omitted.

Because it is ubiquitous. You can virtually guarantee that bash will be found on any arbitrary unix-like system.

Same reason I always cook my eggs using a shoe.

Because a pair of shoes is always standing ready for cooking eggs in, in 99.9% of all homes?

Re: Safe ways to do things in bash

#182

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.

Unfortunately not. I have used Ruby a little bit and that is really nice feature.

Re: Safe ways to do things in bash

#183
post #153

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

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

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

#184

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.

Check out python sh: https://github.com/amoffat/sh

Re: Safe ways to do things in bash

#185

Earlier quoted context omitted.

Do Makefiles fall under that category?

Chiming in for myself, no. Makefiles are more about dependency graphs and reproducible pipelines. I use them to make more sophisticated things than bash scripts, which are purely imperative. I very much dislike cmake, and I've not yet been impressed by any of the other many Make replacement candidates. How else would you accomplish what a Makefile would?

I think for C compilation, Tup is probably better than make. It also has a couple of small syntax improvements compared to make. In particular, it's much better with commands that loop over targets and dependencies.

However, as someone who doesn't write much C, I went back to make. Makefiles seem to adapt really well to different workflows.

A lot of my makefiles look like "download this publically available data, scp this secret data off a company server, mungle all the data with some scripts, shallow clone this branch of that repository, run the program in the repository a few times under different conditions, make a chart of the results".

Tup really doesn't like steps like "clone this repository". Make isn't exactly great with keeping track of targets that output entire directories, but it does work.

I also feel like, for me, make has become more powerful over time as I've learned other tools. Specifically: being able to just magic up any old environment using Nix and be confident that it won't break, mashing data with things like jq, cut, sed and awk, and quick database operations with SQLite.

Re: Safe ways to do things in bash

#186
post #153

Earlier quoted context omitted.

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

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/

Great read, thanks.

Re: Safe ways to do things in bash

#187
post #173
post #171

Earlier quoted context omitted.

Availability is a big issue. It is nearly impossible to get a seasoned sysadmin to install fish for you when bash is available.

If you're convinced your productivity may change by ditching bash, I'm not sure what kind of counterargument the sysadmin will bring.

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

#188

Earlier quoted context omitted.

You just add quotes around $dir. Bash understands the quotes properly/recursively. parent_dir = "$(basename "$dir")"

So I finally found the answer. Thanks.

Bear in mind (pointed by the article as well) that variable assignment doesn't allow spaces around the equals sign, and that in this case the outer parentheses are unnecessary anyway.

Re: Safe ways to do things in bash

#189
post #154
post #152

Earlier quoted context omitted.

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

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.

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

#190
post #172

Earlier quoted context omitted.

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.

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

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.
Post reply on HN