Earlier quoted context omitted.
sh and bash feel pretty primitive after learning PowerShell.
I feel like powershell hides too much to be used regularly. I have a dozen of small shellscripts and aliases to do basically what PS help me to do when i work on windows (and some), but at least i know how it work behind. I had to work with Sencha/ExtJS early 2010. It was the same feeling. Yes, it is powerfull, but too much magic happen for something without a clear orientation (at the time, now it is used for data l…
Shell script best practices, from a decade of scripting things
421–430 of 500 posts
Re: Shell script best practices, from a decade of scripting things
#422If you are on Windows or Linux, Powershell is a decent scripting language that comfortably replaces Shell for scripted task running. The commands are vastly more readable and you get an okay experience with branches. I'd also say that in most cases Python is also a better choice, especially when you use the ! syntax.
Re: Shell script best practices, from a decade of scripting things
#423Earlier 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…
Off topic. What's your opinion on Python? I also write shell scripts, but I'm just curious what you would think about a comparison.
Re: Shell script best practices, from a decade of scripting things
#424If you are on Windows or Linux, Powershell is a decent scripting language that comfortably replaces Shell for scripted task running. The commands are vastly more readable and you get an okay experience with branches. I'd also say that in most cases Python is also a better choice, especially when you use the ! syntax.
The issue I have with powershell is the extreem verbosity. But that's just a personal thing and not something that I can realy blame the language.
gci *.txt | %{ $tot += $_.length }; echo $tot
That's not more verbose than bash (one way of doing this): ls -l *.txt | awk '{ tot += $5 } END { print tot }'
So you'll see the pipeline written (in an example, for clarity), more like: Get-ChildItem *.txt | ForEach-Object { $tot += $_.length }; Write-Output $tot
But that's not how you'd usually use it; until/unless you're putting it in a script.Note that 'ls -l' and guessing that you want to total up field 5 is brittle in way that the Powershell snippet isn't, but I'm leaving that issue aside.
Re: Shell script best practices, from a decade of scripting things
#425And the right-hand-side of a variable assignment.
And the WORD in a case statement. (Not in the patterns, though).
Plus a bunch of other single-token(?) contexts.
I don't recommend relying on the context though, it's clever and makes it hard to verify that the script does not have expansion bugs.
Re: Shell script best practices, from a decade of scripting things
#426Earlier quoted context omitted.
Now do an associative array containing another associative array.
Flatten the damn thing and process it relationally. Linear data scans and copying are so fast on modern hardware that it doesn't matter. It's counterintuitive for people to learn that flattened nested structure with massive duplication still processes faster than that deeply nested beast because you have to chase pointers all over the place. Unfortunately that's what people learn at java schools and they get stuck wi…
Re: Shell script best practices, from a decade of scripting things
#427Instead of implementing a -h or --help, consider using some code like "if nothing else matches, display the help". The asterisk is for this purpose. while getopts :hvr:e: opt do case $opt in v) verbose=true ;; e) option_e="$OPTARG" ;; r) option_r="$option_r $OPTARG" ;; h) usage exit 1 ;; \*) echo "Invalid option: -$OPTARG" >&2 usage # call some echos to display docs or something... exit 2 ;; esac done
Re: Shell script best practices, from a decade of scripting things
#428This is not a best practices guide, please look forward to: https://mywiki.wooledge.org/BashGuide For example, using cd "$(dirname "$0")" to get the scripts location is not reliable, you could use a more sophisticated option such as: $(dirname $BASH_SOURCE)
This is the way. I'm more likely to use the BashFAQ though for actual snippets: https://mywiki.wooledge.org/BashFAQ I start scripts from the very useful template at https://bash3boilerplate.sh/
Re: Shell script best practices, from a decade of scripting things
#429* Write help text to stdout, not stderr, so I can grep it
* Set exit status to 0 on success and 1 or some other small positive integer on failure so I can use || and &&
Re: Shell script best practices, from a decade of scripting things
#430Earlier quoted context omitted.
How do you use dmenu for your shell script? to launch it? to prompt the user for input while it's running? Do you have an example of a script you wrote?
Yes, for creating ad-hoc mini-UIs so the user can select an option. Same with fzf, but it's terminal-bound (rather than X-bound). The scripts are similar to this one: https://github.com/debxp/dmenu-scripts/blob/master/dmenu-kil...