Live data from Hacker News

Shell script best practices, from a decade of scripting things

sharats.me

421–430 of 500 posts

Re: Shell script best practices, from a decade of scripting things

#421
post #125

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…

[deleted]

Re: Shell script best practices, from a decade of scripting things

#422

If 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.

Powershell works great on MacOS; it's my primary shell.

Re: Shell script best practices, from a decade of scripting things

#423

Earlier 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.

I’m not fan of Python, however that’s down to personal preference rather than objective fact. If Python solves a problem for other people then who am into judge :)

Re: Shell script best practices, from a decade of scripting things

#424
post #145

If 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.

The verbosity is optional; most examples you see will be verbose in an effort to be more clear (i.e. Get-ChildItem vs 'gci') but when you have a little experience are typing with Powershell, you'll find the verbosity basically goes away, because you'll be familiar with using aliases and because you won't need abstruse tools and sublanguages (which are more verbose) to do filtering and processing:

  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

#425
> 9. Always quote variable accesses with double-quotes. > - One place where it’s okay not to is on the left-hand-side of an [[ ]] condition.

And 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

#426

Earlier 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…

Then what I need is tuple on bash

Re: Shell script best practices, from a decade of scripting things

#427

Instead 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

My request: usage on error must be output to STDERR, but -h must be to STDOUT

Re: Shell script best practices, from a decade of scripting things

#428

This 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/

Also this http://wiki.bash-hackers.org/start

Re: Shell script best practices, from a decade of scripting things

#430

Earlier 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...

Thanks, I will definitively use that kill one.
Post reply on HN