Finding out if you're inside "script"

John Sellens jsellens-Iv5KO+h6AVB+Y12zHexnB0EOCMrvLtNR at public.gmane.org
Tue May 22 04:04:06 UTC 2007


| This might work:
| 
|   if [ "$(ps -Ao pid,comm | grep $PPID | awk '{print $2}')" = "script" ]
|   then
|     # Change prompt appropriately
|   fi
| 
| Would this be a reliable way to determine if the shell was the child
| of a "script" command?

What about looking at /proc/$PPID/exe? e.g.
  ls -l /proc/$PPID/exe | grep /script

Or change your ps command to "ps -p $PPID -o pid,comm"?

Or something with "lsof" and look for script, such as
"lsof `tty`" or "lsof -p $PPID"?

What happens if you're several levels deep, and some
ancestor is script?  Looking at $PPID probably won't
help there.  Or do you also "export UNDER_SCRIPT=yes"
to help with those cases?

Seems a shame that script(1) doesn't set some environment
variable.

On the other hand, script(1) is often used to show what
normally happens, and if you do something differently
under script(1), do you invoke a "heisenbug"?

At UWaterloo, many years ago, Dan Salomon wrote a
scriptfix(1) command that removes escape sequences,
and backspaces, and carriage returns and such from
script(1) output files.  I often find that the post-process
approach suits me well.

Hope that helps!

John
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list