News for hackers. Stuff that matters.
Re:Minimal safe Bash script template by traceroute66 on Tuesday December 15, 2020 @07:39AM (#25429713)
I think its almost hilarious that something calling itself a "safe script template" doesn't even spend a couple of lines dealing with the possibility of race conditions (e.g. a locking process so that if, as is almost inevitable, my script gets put in a cronjob, it doesn't run when already running).

The four lines the script wastes on setup_colors() - something that has nothing to do with "safety" - could easily be used for a basic locking process.

Re:Minimal safe Bash script template by michaelcampbell on Tuesday December 15, 2020 @07:43AM (#25429748)
While I find the "almost hilarious" kind of overly snarky, I have my own version of this that has the race condition check. Basically uses `mkdir $THISBIN.lock` line that gives me an atomic way to create or check for existence of a logical semaphore, and provides me with a scratch area. The trap'd cleanup function removes it (which when debugging, and I use that scratch area, is inconvenient, but <shrug>)
Re:Minimal safe Bash script template by twic on Tuesday December 15, 2020 @08:17AM (#25429986)
Read to the end before screaming. I do this:

  exec >>$log_file 2>&1
  if ! flock --exclusive --nonblock 1
  then
    log "unable to get exclusive lock, aborting"
    exit 1
  fi
Which redirects standard output to a log file, then tries to get an exclusive lock on it. The lock is created by the flock subprocess, but continues to exist until the script exits and closes that file descriptor.

This means there can only be one process writing to a given log file at a time. For me, that is the right granularity of locking - i have several cron jobs running this script, but with different arguments, and writing to different log files.

Re:Minimal safe Bash script template by michaelcampbell on Thursday December 17, 2020 @09:55AM (#25456087)
I like the idea of a separate lock/semaphor per input args. Haven't needed that yet but I can see where I might.
Re:Minimal safe Bash script template by AdamGibbins on Tuesday December 15, 2020 @05:54PM (#25436447)
flock isn't all platforms unfortunately, doesn't exist on MacOS.
Re:Minimal safe Bash script template by twic on Wednesday December 16, 2020 @02:15AM (#25439919)
Quite true. On OS X, this script will do the right thing and not run until you get a proper computer.
Re:Minimal safe Bash script template by mradzikowski on Tuesday December 15, 2020 @11:16AM (#25431628)
Well, I agree with your cron argument. On the other hand - sometimes I have some utility scripts, for example to trigger some processing, that I want to run multiple times at the same time, with different arguments. So lock is not a universal thing for me. (I'm the article author btw)
Re:Minimal safe Bash script template by michaelcampbell on Thursday December 17, 2020 @09:56AM (#25456090)
Sure; my template is a one-size-fits-all thing, so if I know I'm not worried about race conditions for a file I just remove that bit.
Re:Minimal safe Bash script template by dvfjsdhgfv on Tuesday December 15, 2020 @07:51AM (#25429797)
This is my feeling, too. Race conditions, misunderstanding globbing, whitespace in path names - these are the main sources of errors in Bash scripts I come by.
Re:Minimal safe Bash script template by Silhouette on Tuesday December 15, 2020 @08:37AM (#25430147)
whitespace in path names

And more generally, the arcane rules for quoting, substitutions, parameter parsing, running other processes, and how all of these interact.

Re:Minimal safe Bash script template by oblio on Tuesday December 15, 2020 @11:05AM (#25431483)
What's the best way to handle race conditions?
Re:Minimal safe Bash script template by hiq on Tuesday December 15, 2020 @12:23PM (#25432434)
Re:Minimal safe Bash script template by oblio on Tuesday December 15, 2020 @02:13PM (#25433836)
Interesting, but it looks like an external dependencies, not something that's installed by default :-(
Re:Minimal safe Bash script template by hiq on Tuesday December 15, 2020 @03:50PM (#25435074)
By default where? I think it comes with coreutils, so for instance it'd be pretty hard to get a Debian installation without it, but maybe you have a more exotic system?
Re:Minimal safe Bash script template by oblio on Tuesday December 15, 2020 @04:15PM (#25435363)
Hmmm, maybe I need to check again. Thanks for the pointer!