This can be done using the `ln` command:

tempfile=$(mktemp ./lock.XXXXXX)
lockfile=./lockfile
trap "rm $lockfile" EXIT
try=1
while ! ln $tempfile $lockfile > /dev/null 2>&1; do
    try=$[ $try + 1 ]
    if [[ $try -gt 10 ]]; then
        echo "ERROR: Timeout trying to lock $cfgfile"
        rm $tempfile
        exit 1
    fi
    sleep 1
done
rm $tempfile
# Protected section

I work as a freelancer, so if you don’t want to do that kind of things yourself or don’t have the time, just drop me a line to hire me.