Live data from Hacker News

Pure Bash Bible

github.com

241–250 of 258 posts

Re: Pure Bash Bible

#241
post #149

Earlier quoted context omitted.

I highly recommend running shellcheck on all of your bash code. It is what finally taught me the practical difference between [[ and [. There are some tests that will always pass in [, for example, but work properly in [[; one project never realized.

My (personal) better recommendation is to avoid bash scripts whenever possible ;) I generally tell people that, once a script is longer than ~100 lines and/or you start adding functions, you're probably better off with something like Python. I know that's not a popular opinion with shell enthusiasts, but it's saved me so much frustration both in writing new scripts and coming back to them later for refactoring.

Bash is a typical Unix tool, and Unix follows the KISS principle. Hence, also Bash scripts should be KISS.

If you need >100 lines of code your code is too complex in the script world, and you should split it into several scripts where each does one thing, and that one thing well. Usually, bash scripts are applied with pipes. A sequence like cat x | tr a b | sort >output is much more likely and easy to handle then a single script which does all these things.

KISS with Bash is a very different approach compared to other scripting languages like Python and Perl. There you can write long code easily and conveniently. However, things can get tough when larger scripts need to be maintained.

I consider the examples in the "Bash Bible" a collection of useful black boxes. It's fine if they just work. Regarding the "unreadable" trim_string for instance, if you have problems to understand that code, and you have to change something then you can simply write your own new trim_string script, even in Python or Perl if you like. Pipes work also well with them.

Update: Another advantage of bash and pipes over Python/Perl is that the Unix system can assign each script in a pipe to a separate thread. That means, simple bash scripts with pipes can work _much_ faster than single scripts in Python or Perl.

Re: Pure Bash Bible

#242
post #157

This seems like a good time to mention my (ridiculous) project, a ctypes module for bash. https://github.com/taviso/ctypes.sh/wiki There are some little demos here: https://github.com/taviso/ctypes.sh/tree/master/test I even ported the GTK+3 Hello World to bash as a demo: https://github.com/taviso/ctypes.sh/blob/master/test/gtk.sh

This is MARVELOUSLY ridiculous and I love it. Thanks for sharing your work.

Re: Pure Bash Bible

#243
Very useful. I hate having to pull in external processes to do something simple in BASH that the manpage doesn't cover. Expecting PERL to be on every machine (or the right version of SED.. posix? gnu?) is not reliable in my line of work.

Re: Pure Bash Bible

#244

I have a personal dislike for regexes and non human readable code, it gives maintenance headache. It's why I avoid shell scripts as much as possible. The first example is not human readable, if the name of the function is a lie, I have no idea what this piece of code do : trim_string() { : "${1#"${1%%[![:space:]]*}"}" : "${_%"${_##*[![:space:]]}"}" printf '%s\n' "$_" }

"maintenance headache."

Then stop maintaining code from languages you don't understand. This is frustrating. I've seen solutions on the comments that call PYTHON! Are you effing kidding me? PYTHON ??

Granted, BASH docs aren't particularly succinct, but shell scripts are an absolute necessity in the OS world.

It's pretty simple: If you don't understand shellcode, don't maintain an OS, or rather, don't expect to be accommodated for lack of knowledge for something that's been standard for 30+ years.

Re: Pure Bash Bible

#245
post #221

Earlier quoted context omitted.

a "real language" would be any programming language that is primarily designed and presented as a programming language.

Nice recursive definition.. The practice of programming in shell languages was well established when Bash was designed and Bash was definitely designed with that use in mind. So by your definition Bash is a "real" programming language. Lisp on the other hand, was much more designed as a system and formal notation to reason about certain classes of logic problems.. So, Lisp is not a "real" language?

By "programming language" he probably means something that has robust flow control mechanisms and metaprogramming facilities.

You can definitely tell there's a different "feel" to bash and Tcl, Python, Perl, Go, etc, yes? Shell languages basically evolved out of batch processing languages that were meant to only run programs in sequence and it shows.

Re: Pure Bash Bible

#247

I have a personal dislike for regexes and non human readable code, it gives maintenance headache. It's why I avoid shell scripts as much as possible. The first example is not human readable, if the name of the function is a lie, I have no idea what this piece of code do : trim_string() { : "${1#"${1%%[![:space:]]*}"}" : "${_%"${_##*[![:space:]]}"}" printf '%s\n' "$_" }

Is the following a maintenance headache, or non-human readable? #!/bin/sh FOO=" some long string here " FOO="$( echo "$FOO" | sed -e ' s/^[[:space:]]//g; s/[[:space:]]$//g ' )" This isn't "pure bash", but most of what I write in shell scripts isn't "pure bash". It's shell scripting: dirty, slow, easy, effective. Like any 'language', it takes on the complexity you put into it. English is really complicated, but you ca…

Unlike the original code, this strips whitespace from every line of input.

Re: Pure Bash Bible

#248
post #190

Earlier quoted context omitted.

Is the following a maintenance headache, or non-human readable? #!/bin/sh FOO=" some long string here " FOO="$( echo "$FOO" | sed -e ' s/^[[:space:]]//g; s/[[:space:]]$//g ' )" This isn't "pure bash", but most of what I write in shell scripts isn't "pure bash". It's shell scripting: dirty, slow, easy, effective. Like any 'language', it takes on the complexity you put into it. English is really complicated, but you ca…

> echo "$FOO" | sed -e ' s/^[[:space:]]//g; s/[[:space:]]$//g ' Your example fails when $FOO is "-n", for instance. Also the g modifiers are redundant in your example since there's only one beginning of line per line and one end of line per line. I would instead write this: sed -r 's/^\s+|\s+$//g' EDIT: I think I see now what you probably thought would happen by using g, but no it wouldn't remove multiple spaces. So,…

Beware that -r is a GNU-specific option.

Re: Pure Bash Bible

#249

Earlier quoted context omitted.

It's more an annoyance than really a problem and stick with them rather than Amazon. Leanpub looks like a nice enough bunch and you, as the author, receive most of the proceeds. I'm probably anyway the exception nowadays not being able to access private email remotely. But what you may point out to them is that it may be worthwhile to think through their checkout process. Also for their own benefit and the benefit of…

I'm a bit puzzled how you've ended up with a system where you can't access email remotely. That probably took a lot more time than just creating a gmail account or even using your own domain but hooking it up to google for all the boring stuff (like, uh, making it available remotely). Although you mention "private" email so perhaps you also have "public" email which you can access - i'm not sure how I'd configure mai…

My phone sucks (Nokia 8110 4G, where mail conked after the first software upgrade) and working for a bank, which disallows external messaging for regulatory reasons.

Re: Pure Bash Bible

#250
post #89

Earlier quoted context omitted.

I think you're reading my comment the wrong way: I meant to say that doing e.g. piping in Python is a lot of pointless work (pomp), as you agree. This is perhaps one big benefit but not one that is exclusive to a sh-like language. Instead I would like to see a language with strong flow control or metaprogramming capabilities take on processes as a first class citizen. Perl is probably the closest but still has some w…

If you pardon the shameless self promotion; I'm working on something just like that: https://github.com/lmorg/murex It currently has: * Proper error handling (eg try and catch blocks) * unit testing and debugging frameworks to help with development and maintainability * data-type aware, including complex types like how CSV, JSON and YAML are all handled as memory structures and thus the same tools can query any struc…

Seems quite similar to Elvish (https://elv.sh/); have you checked it out?
Post reply on HN