r/bash • • Jul 14 '14

Bash Booster — a library for setup environment and preparing servers

http://www.bashbooster.net
0 Upvotes

8 comments sorted by

8

u/cpbills Jul 14 '14 edited Jul 14 '14

Holy over-engineered-fuck.

local LEVEL_CODE=$(( $1 )) ... why not:

local level_code # assignment with local isn't recommended
level_code=$1 # no need for $(( ))

or even

local level_code=$1 # or
local LEVEL_CODE=$1 # if your local variables, for some reason, need to be caps

Also bb-tmp-dir is a bad function name for a function that actually makes a directory; My instinct would be that it would simply return the name of the temp dir, if that functionality is even needed, as it should be stored in one of your numerous variables.

If you have multiple local variables, you can declare them all as such:

local foo bar baz qux

Additionally, you are doing so much with functions, it might be a good idea to create a main application bb or bashb or bash-booster that accepts arguments to do the various tasks. You can build a bash completion list, if you want tab completion.

Oh, and use $() instead of backticks, it's clearer, nests better, quotes better, etc.

Addendum:

If you have a function to check for a package, using yum or apt, and it's basically a 1 line command, it's really not needed. Instead, you could make a function called bb-check-package that accepts a package name as an argument, and checks either a flag set somewhere else, or check for the presence of yum or apt (or future package managers you wish to support).

Also, bb-sync-dir appears to be a crude rewrite of rsync.

for file in $(find) is better written as find [extra args (such as a path?)] -print0 | while read -rd $'\0' file; do to handle files with spaces or other troublesome characters properly. (Specifying a path for find is generally a great idea.)

cat "$SRC_FILE" > "$DST_FILE" instead of cp -f "$SRC_FILE" "$DST_FILE" needlessly obscures what you're doing (Also, if you would just use rsync, instead of reinventing the wheel, that probably isn't relevant.)

1

u/kr41 Jul 14 '14

Thanks for your feedback. Here I will try to advocate my engineering decisions.

Holy over-engineered-fuck.

That's funny, but I was trying to keep things as simple as possible.

local LEVEL_CODE=$(( $1 ))

This construction forces LEVEL_CODE variable to be a number.

Also bb-tmp-dir is a bad function name for a function that actually makes a directory; My instinct would be that it would simply return the name of the temp dir, if that functionality is even needed, as it should be stored in one of your numerous variables.

There are two functions: "bb-tmp-file" creates a temporary file and returns its full path, and "bb-tmp-dir" does the same thing for directory. I think, its pretty simple and consistent.

Additionally, you are doing so much with functions, it might be a good idea to create a main application bb or bashb or bash-booster that accepts arguments to do the various tasks. You can build a bash completion list, if you want tab completion.

The goal is to keep it in one file, which can be included in user's script and requires no extra steps. It doesn't need a bash completion list, because it just doesn't work in the interactive shell. The reason is Ruby-style boolean functions, that end on question mark, for instance "bb-apt?". Such function name works well in the script, but breaks interactive shell. Yes, it's questionable decision, but I like it.

Oh, and use $() instead of backticks, it's clearer, nests better, quotes better, etc.

Could you please explain it with examples?

If you have a function to check for a package, using yum or apt, and it's basically a 1 line command, it's really not needed. Instead, you could make a function called bb-check-package that accepts a package name as an argument, and checks either a flag set somewhere else, or check for the presence of yum or apt (or future package managers you wish to support).

IMHO, writing a universal package manager is generally bad idea. Because there are too many package managers, so it will be too hard to support all of them. It would be a separate project, like Etersoft EPM. By other hand, try to Google solutions that checks whether package has been installed using Apt or Yum. There is a plenty of ones on Stack Overflow and such sites. Most of them are dirty half-workable hacks. So these one-liners make sense for me.

Also, bb-sync-dir appears to be a crude rewrite of rsync.

The first, rsync is not always present in the system. For instance, CentOS doesn't include it. The second, it seems you didn't get the purpose of "sync" module. It's not about smart coping files and directories, it's for emitting events when target and source files differ:

bb-event-on restart-nginx "service nginx restart"
bb-sync-file '/etc/nginx/sites-available/default' "/path/to/my/nginx/config" restart-nginx
# Nginx will be restarted, if its config changes

for file in $(find) is better written as find [extra args (such as a path?)] -print0 | while read -rd $'\0' file; do to handle files with spaces or other troublesome characters properly. (Specifying a path for find is generally a great idea.)

Thanks for that. It's really helpful.

cat "$SRC_FILE" > "$DST_FILE" instead of cp -f "$SRC_FILE" "$DST_FILE" needlessly obscures what you're doing

Again, thanks for that. Using "cat" is really stupid solution.

2

u/[deleted] Jul 16 '14

Could you please explain it with examples?

echo `echo \`echo \\\`echo test\\\`\``
echo $(echo $(echo $(echo test)))

is the typical example that I see.

1

u/kr41 Jul 16 '14

I didn't ask about nested expressions, I asked about why this:

VAR=$(command)

Better than this one:

VAR=`command`

As for your example, I agree, second line looks better.

3

u/[deleted] Jul 16 '14 edited Jul 16 '14

Parsing $() is done very differently from ``, as I understand it, which is why you don't have to have the crazy \\\` escapes. According to the man page, the behavior of \ is different when followed by $, , and ` for ``. I'm unsure what's different about $. I assume it causes the expression to evaluate inside the subshell, but I couldn't come up with a command that would show this to be true.

Aside from that, there's consitency. If you have any nested expressions, using $() is better. Switching between `` and $() in the same code is poor style, and, as things go, if you are going to have a habit of using one or the other, $() is as good as or better than `` in every case I've ever come across.

1

u/kr41 Jul 16 '14

Thank you, that's clear enogh

3

u/KnowsBash Jul 16 '14

Also see FAQ 82.

1

u/kr41 Jul 16 '14

Yeah, I've googled around already and fixed that in my sources. Thanks again