I agree we need a shell scripting language, I disagree that bash zsh or anything that frequently uses double square brackets and awful program names is the epitome of shell scripting language design.
Shell script best practices, from a decade of scripting things
381–390 of 500 posts
Re: Shell script best practices, from a decade of scripting things
#382Re: Shell script best practices, from a decade of scripting things
#383Earlier quoted context omitted.
I noticed that I became so much more quick after taking 1 hour to properly learn awk. Yes, it literally takes about 1 hour.
Awk is awesome but saying it literally takes 1 hour to properly learn it is a bit overselling.
[1] https://www.gnu.org/software/gawk/manual/gawk.html
For the sarcasm impared among us, everything above this, but possibly including this sentence is sarcasm.
Re: Shell script best practices, from a decade of scripting things
#384Hands down, shell scripting is one of my all time favorite languages. It gets tons of hate, e.g. "If you have to write more than 10 lines, then use a real language," but I feel like those assertions are more socially-founded opinions than technically-backed arguments. My basic thesis is that Shell as a programming language---with it's dynamic scope, focus on line-oriented text, and pipelines---is simply a different p…
Shell and SQL make you 10x productive over any alternative. Nothing even comes close. I've seen people scrambling for 1 hours to write some data munging, then spend another 1 hour to run it through a thread pool to utilize those cores , while somebody comfortable is shell writes a parallelized one liner, rips through GBs of data, and delivers the answer in 15 minutes. What Python is to Java, Shell is to Python. It sp…
* Shell scripts force you to think in a more scalable way (data streams)
* Shell scripts compose rich programs rather than simplistic functions
* Shells encourage you to program with a rich, extensible feature set (ad-hoc I/O redirection, files)
The only times I don’t like shell scripts are when dealing with regex and dealing with parallelism
Re: Shell script best practices, from a decade of scripting things
#385I'm surprised that no one mentioned a pair of tiny functions to log each command before it is run. Nicer versions also print a timestamp. Of course, this setup assumes: set -e echo_command() { echo echo '$' "$@" } echo_and_run_command() { echo_cmd "$@" "$@" } Then something like: main() { # For simple commands that do not use | etc. echo_and_run_command cp --verbose ... # More complex commands echo_command grep ... '…
It does not address timestamps, but set -x does this seamlessly without cluttering up your script. You can even run your script with sh -x script If you didn't always want the logging output.
#!/usr/bin/env bash
(
foo
bar
) 2>&1 | print_and_log "$logfile"Re: Shell script best practices, from a decade of scripting things
#386 cd "$(dirname "$0")"
part of this? This is changing to the directory of where the script is all cases?EDIT: I should've just tested this to see :) I did and it does exactly that. Very helpful. I didn't realize $0 is always the first argument. Kind of like how `self` is the first implicit argument in OOP methods?
Re: Shell script best practices, from a decade of scripting things
#387Earlier quoted context omitted.
yeah thats exactly right. it may only take an hour to learn, but every time i need to use awk it seems like i have to spend an hour to re-learn its goofy syntax.
alas this is true, I never correctly recall the order of particular function args as they are fairly random, still beats the alternative of having to continually internalize entire fragile ecosystems to achieve the same goal.
Re: Shell script best practices, from a decade of scripting things
#388Earlier quoted context omitted.
I tried PowerShell, hated it. The idea of manipulating objects instead of text streams is interesting, and avoids most of the footguns sh/bash have, but you also lose in flexibility. One reason is that there are thousands of command line tools in the UNIX ecosystem that process text streams and are designed to work with shells like bash. You have much less options when you are processing PowerShell objects. Note: I t…
ls -File | % { $fh = Get-FileHash $_ SHA1; if ($_.Name -match $fh.hash) {$_} }
Anyways, it was a long time ago, maybe I will give PowerShell a retry at some point.
Re: Shell script best practices, from a decade of scripting things
#389Earlier quoted context omitted.
> getting the shell quoting hell right Shameless plug coming, it this has been a pain point for me too. I found the issue with quotes (in most languages, but particularly in Bash et al) is that the same character is used to close the quote as is used to open it.m. So in my own shell I added support to use parentheses as quotes in addition to the single and double quotation ASCII symbols. This then allows you to nest…
One of my favorite Perl features that has been disappointingly under-appropriated by other languages is quoting with q(...).
Though Ruby makes it confusing AF because there are two quoting types for both strings and symbols, and they're different. (%Q %q %W %w %i) I can never remember which does which.... the letter choice feels really arbitrary.
Re: Shell script best practices, from a decade of scripting things
#390Earlier quoted context omitted.
ls -File | % { $fh = Get-FileHash $_ SHA1; if ($_.Name -match $fh.hash) {$_} }
Doesn't work, it is not recursive and it was a CRC, not SHA1 but I probably could have found a solution starting from there. Anyways, it was a long time ago, maybe I will give PowerShell a retry at some point.