News for hackers. Stuff that matters.
Re:Some advanced bash scripting by Xophmeister on Friday November 11, 2016 @11:12AM (#12931927)
This is kind of lacking in content for something claiming to be "advanced", to say the least. "Defensive BASH programming"[1], which has been featured on HN before, contains a lot more useful advice.

Personally, I would also recommend using ShellCheck[2] to lint your scripts.

[1] http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-progr...

[2] https://www.shellcheck.net/

Re:Some advanced bash scripting by jdorfman on Friday November 11, 2016 @11:17AM (#12931971)
These are great resources, thank you.
Re:Some advanced bash scripting by prairiedock on Friday November 11, 2016 @11:36AM (#12932151)
It's not advanced, it's not long enough, and it's not correct either.

> This will set the value of TMP to `/tmp/myproject` only if it has not been set.

This should read "if", not "only if". And it should read: "This will set the value of TMP to `/tmp/myproject` if it has not been set or if it is null."

The form that means 'if it has not been set' has no colon; it is

  TMP=${TMP-/tmp/myproject}
  
See https://www.gnu.org/software/bash/manual/bash.html#Shell-Par...
Re:Some advanced bash scripting by cosmie on Friday November 11, 2016 @11:23AM (#12932045)
The first example has a lot more use cases than it alludes to. I have a lot of aliased utility scripts for text processing, and frequently need to use them over entire files as well as on output piped from other processes. In this case, I don't know if the input file is going to be an argument or stdin from some upstream operation. That's easily taken care of with

  INPUT_FILE="${1:-/dev/stdin}"
Which sets the input to the first argument if there is one, or stdin if there isn't. My solution was a lot messier before I learned about parameter substitution.
Re:Some advanced bash scripting by joatmon-snoo on Friday November 11, 2016 @11:19AM (#12932002)
Also, set -e is not a good flag to rely on, because it can really trip you up if you do anything at all that involves subshells, e.g.:

  set -e

  function foo {
      false
      echo foo
  }

  i=1
  while [[ i -le 20 ]]; do
      echo "$i $(foo)"
      i=$((i+1))
  done
Re:Some advanced bash scripting by robinhoodexe on Friday November 11, 2016 @11:09AM (#12931896)
Interesting, but a whole lor shorter than what I expected to be honest.