1 2 3 4 5 6 7 8 9 10 11 12 13 14
lock() { if ! mkdir "$1" &>/dev/null; then while true; do sleep 2; if [[ ! -d "$1" ]]; then return fi done fi; } lock $1