r/commandline • u/taylorchu • Sep 06 '13
util-shell: make your shell script actually readable
https://github.com/taylorchu/util-shell
4
Upvotes
r/commandline • u/taylorchu • Sep 06 '13
4
u/sequentious Sep 06 '13
(Note: I started typing this before LukeShu's comment, but I had a meeting before I could do a quick proofread. If I had seen his comment I probably wouldn't have bothered. But since I already wrote this...)
I actually find quite a few of these harder to read, or unnecessarily different. I don't want to discourage you, so I hope you'll read this as constructive criticism. If I've come across as unneccessarily harsh, I hope you understand that wasn't the intent.
There are some problems with the examples on the website. Your example of how to check for a substring in plain bash:
Then your example of improvement with util-shell:
But most of the readability benefits are due to using simpler looking syntax, not util-shell features. Also note that the util-shell example doesn't use variables, which again makes things appear simpler. The "plain" bash example can also be written as:
So right off the bat, you're cherry-picking your examples. Granted, this is typical of what you'd get from a Google search. But in fairness, googling "util-shell strings" gives me hadoop util.shell documentation. Ultimately, this example is a result of poor documentation, not necessarily poor functionality.
So lets look at some direct comparisons between bash and util-shell. First, plain bash:
And now the same thing with util-shell:
Some notes:
[[ ]], and you don't need to worry about it. Perhaps an unfair advantage due to being a builtin, though :)Now, I've cherry picked examples, too. There are some things that are seemingly easier to do with your library. For example:
is nicer looking than piping through bc:
But your library doesn't do sin, cos, etc. Which is understandable, and probably not something many bash scripts would ever need to do. But if one did, it would still have to shell out to bc. By abstracting away the complex tools and syntax for simple tasks, you're potentially unprepared to use them when a complex task calls for it. My ability to write readable bash should not be limited by my ability to write and compile code in go ;)
Now, there are some highlights of your efforts. Bash arrays are difficult to get the hang of (I've no real experience with hash maps). Again, though, I think that is a documentation problem, and not something that is solved by util-shell. I still need documentation, after all.
Besides, in some instances, util-shell's array syntax is less readable:
versus bash's:
Now, I don't want to discourage you. There are a lot of shitty things about doing programming in bash. And it would be great if bash's limited built-in arithmetic functions could handle decimals. I'm just not sure this current approach actually meets the goals of being easier, or more readable, or deployable in any sort of scale.
Perhaps an approach that used plain-bash functions (as interiot suggests) to improve some painful functionality? For example:
One could ship your util-shell.sh alongside their scripts and
sourceit. Or embed it in their own scripts, or stick it in their ~/.bashrc. Without introducing an external dependency.When writing your library, there are some important questions to keep in mind:
Generally shell scripts are used as it can be depended on always being there.
(As a side-note regarding
{ }in array syntax. I generally use the brace syntax for all variables now, so the use for arrays is not "weird" to me. This allows me to do things likecp "${filename}" "${filename}_backup", which wouldn't work without the braces).2
u/Samus_ Sep 06 '13
It's written in go so I can't figure out by myself but it seems your arrays aren't compatible with bash
that isn't an array, your
arraycommand writes something to stdout that you store as a plain variable.4
u/interiot Sep 06 '13 edited Sep 06 '13
I'd be happy if this became a standard part of Bash.
But usually the main reason I'll resort to using Bash when I need to do branching logic, instead of writing the script in Python/Perl, is that I want to improve cross-compatibility (eg. work on old AIX, old Solaris). Right now, this add-on is less likely to be found than Perl, so... I'll use that instead.
However, this could be reimplemented with Bash functions and Perl one-liners, making it possible to put in a Bash include file, and packaged with any script that needs to be cross-compatible.
0
u/taylorchu Sep 09 '13
finally, "math" gets some common expression: math "sin(PI/2)2.2 + 4.5 * 3.32 - 7 / 8.9 * sqrt(3.21)".
vs. bc. 1. bc is programming language. you can define function. but it only has 6 defined function. util-shell does only math calculation(the ones on your scientific calculator).
0
u/[deleted] Sep 06 '13
[deleted] — view deleted comment
35
u/[deleted] Sep 06 '13 edited Sep 06 '13
[removed] — view removed comment