in reply to Can a script tell if it is being run from inside a script versus in an interactive terminal session?

So I was thinking about this some more while waiting for my wife to pick me up. I'm not crazy about making system calls, but it's not a critical feature and I could error-check for the existence of the system commands and if it's not present, I can just make an arbitrary decision. But here was my idea:
sleep 10 | perl -e '$ppid=getppid();$sibs=`pgrep -P $ppid`;print("MY P +ID: $$\nPARENT: $ppid\nSIBLINGS: $sibs\n")' MY PID: 79496 PARENT: 563 SIBLINGS: 79495
It doesn't work all the time. If I run a short command, like echo, I don't get that PID:
echo test | perl -e '$ppid=getppid();$kids=`pgrep -P $ppid`;print("MY +PID: $$\nPARENT: $ppid\nSIBLINGS: $kids\n")' MY PID: 79493 PARENT: 563 SIBLINGS:
Plus, this doesn't tell me about whether or not I'm running inside a shell script. I couldn't find a perl module that does what pgrep does.
  • Comment on Re: Can a script tell if it is being run from inside a script versus in an interactive terminal session?
  • Select or Download Code