Sample Header Ad - 728x90

Flock and bash strange chicken and egg problem

2 votes
1 answer
108 views
In which order are the distinct steps of this bash command done:
(flock -n 9) 9> toto.txt
If I do only the subshell part:
(flock -n 9)
I get this result: flock: 9: Mauvais descripteur de fichier (Wrong file descriptor). Hence, I would assume that the subshell spawn opens the file descriptor 9 first with (...) 9> toto.txt. But If I do:
(ls -l /proc/$$/fd) 9> toto.txt 
total 0
lrwx------ 1 laurent laurent 64 déc.  16 00:24 0 -> /dev/pts/2
lrwx------ 1 laurent laurent 64 déc.  16 00:24 1 -> /dev/pts/2
lrwx------ 1 laurent laurent 64 déc.  16 00:24 2 -> /dev/pts/2
lrwx------ 1 laurent laurent 64 déc.  16 00:24 255 -> /dev/pts/2
The file descriptor 9 is not listed. Hence it seems that flock is responsible for opening it? Can someone explain what are the steps and their order for the "handshake" between the inside of the subshell and the outside of it?
Asked by Laurent Lyaudet (137 rep)
Dec 16, 2023, 12:05 AM
Last activity: Dec 16, 2023, 11:48 AM