0

I have a shell script that runs a function that exists in another shell script.

#!/usr/bin/env bash    
source docker_util.sh
log_exec docker_login

#More stuff here 

My function looks like following.

#!/usr/bin/env bash
docker_login() {
    counter=1
    while [ $counter -le 10 ]
    do
         if { set -C; 2>/dev/null >/var/tmp/docker-credstore-lock.lock; }; then
           break
         else
           sleep 10
           ((counter++))
         fi
    done

    #Run some stuff here

}

I am looking for the best way to delete the docker-credstore-lock.lock file at the end of executing docker_login function. I am aware of using trap "rm -f /var/tmp/docker-credstore-lock.lock" EXIT , but the issue with this is this only runs once the original shell script is exiting, so the lock will be there even after the docker_login method is finished.

I have also thought of using following block in the docker_login method, but this will not be executed in a ctrl+c event, which is problematic.

#!/usr/bin/env bash
docker_login() {
    counter=1
    while [ $counter -le 10 ]
    do
         if { set -C; 2>/dev/null >/var/tmp/docker-credstore-lock.lock; }; then
           break
         else
           sleep 10
           ((counter++))
         fi
    done

    {
        #Run some stuff here
     } || {rm -f /var/tmp/docker-credstore-lock.lock}

}

Any idea on the best way to achieve this?

David Maze
  • 130,717
  • 29
  • 175
  • 215
Chamila Wijayarathna
  • 1,815
  • 5
  • 30
  • 54
  • What you call _lock_ is a file. If you delete a file in one script, it does not pop up magically in a different file. You don't even seem to use `flock`, but just rely on `set -C` as a guard (can't tell how reliable this is). – user1934428 Nov 25 '22 at 11:34
  • My system is on OSX. From what I read, flock seem to be not supported for OSX. Is there any alternative? – Chamila Wijayarathna Nov 27 '22 at 06:31
  • This seems to have been discussed [here](https://stackoverflow.com/questions/10526651/mac-os-x-equivalent-of-linux-flock1-command). – user1934428 Nov 27 '22 at 09:31
  • Just an idea: Would perhaps [chflags](https://ss64.com/osx/chflags.html) help? – user1934428 Nov 27 '22 at 09:38

1 Answers1

0

You could use the second solution AND trap the KILL signal in the script of docker_login.

I mean

#!/usr/bin/env bash
trap "rm -f /var/tmp/docker-credstore-lock.lock" KILL

docker_login() {
    counter=1
    while [ $counter -le 10 ]
    do
         if { set -C; 2>/dev/null >/var/tmp/docker-credstore-lock.lock; }; then
           break
         else
           sleep 10
           ((counter++))
         fi
    done

    {
        #Run some stuff here
     } || {rm -f /var/tmp/docker-credstore-lock.lock}

}

By the way, I think the rm command in the docker_login won't ever executed if the previous block exit with false value

  • As per my understanding, the rm block only gets executed if there is a false exit in the first block before ||. If it returns successfully, `rm` will not get executed. – Chamila Wijayarathna Nov 27 '22 at 22:15