I'm trying to learn how to work with proc and file descriptors. I want to start a process that opens a file, read the same file in parallel through a file descriptor from proc.
I tried to do this:
#!/bin/bash
echo "Hello, world!" > script.txt
cat script.txt &
pid=$!
sleep 1
cat "/proc/$pid/fd/3"
kill $pid
I got this result:
Hello, world! cat: /proc/5087/fd/3: No such file or directory ./script.txt: line 13: kill: (5087) - No such process
Perhaps the problem lies in opening the file using cat, but I do not know what it can be replaced with so that the file is opened before the attempt to read through the file descriptor. Please tell me how to fix this.