Home > OS >  Suppress mkdir warning while still executing the OR option?
Suppress mkdir warning while still executing the OR option?

Time:11-24

I have a bash script that sets a directory as a lock, and if the lock is in place then it send a message to the user who attempted to run it.

I'm wondering if its possible to somehow suppress the "directory already exists" message, but still run the other function (warning_run_in_place). Because if using the -p flag on mkdir it would not execute the warning_run_in_place portion.

Essentially it's something like

mkdir MYLOCK || warning_run_in_place

warning_run_in_place() 
{
  echo "Hey I'm already running..."
  exit 1;
}

CodePudding user response:

You should replace:

mkdir MYLOCK

by:

mkdir MYLOCK 2>/dev/null

CodePudding user response:

Suggesting -p option in mkdir command (as documented here):

  mkdir -p MYLOCK || warning_run_in_place
  • Related