Linux – Is it possible to see the original contents of a running but modified bash script?

Is it possible to see the original contents of a running but modified bash script?… here is a solution to the problem.

Is it possible to see the original contents of a running but modified bash script?

Let’s say I run a bash script run.sh in the background, and then I modify the run.sh.
Is there a way to see the original contents of run.sh script?

For example:

echo "echo a1; sleep 120; echo a2" > run.sh; bash run.sh& 
aPid=$!
echo "echo bbbbbb" > run.sh

I’m expecting something like cat {magic file/path}, which will print

echo a1; sleep 120; echo a2

I’m searching for /proc/$aPid but can’t find anything.

Solution

bash seems to be trying to put the script it’s currently interpreting on file descriptor 255, at least on my system, without any non-default constraints on file descriptor max etc… This may not be guaranteed, so you might have to play around a bit, but given the running PID bash /some/script/sh process, look at /proc/<PID>/fd/ In the file descriptor link – 0, 1, and 2 are of course predefined stdin/stdout/stderr, and there may be others depending on what your script does, but sifting through its contents shouldn’t be too difficult to figure out which file descriptor corresponds to your script file, you can see the original script than cat or anything else (e.g. cat /proc/12345/fd/255).

Related Problems and Solutions