简体   繁体   中英

Suppress mkdir warning while still executing the OR option?

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;
}

You should replace:

mkdir MYLOCK

by:

mkdir MYLOCK 2>/dev/null

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

  mkdir -p MYLOCK || warning_run_in_place

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM