Finding out if you're inside "script"

Chris F.A. Johnson cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Tue May 22 04:16:52 UTC 2007


On Mon, 21 May 2007, Giles Orr wrote:

> I'm looking at updating the "Bash Prompt HOWTO," and one thing that's
> frustrated me is the ability to detect whether or not you're inside a
> "script" shell.  Being able to determine if you're inside "screen" is
> useful, particularly if you don't use a status bar, because you can
> have the prompt modify itself if it detects either TERM=screen or
> STY={something}.  Fairly easy because "screen" changes the
> environment.  But inside "script" the only settings that change are
> SHLVL and PPID.  To use SHLVL you'd have to start laying breadcrumbs,
> and that would be very tricky ...  It's useful though because a
> colourized prompt is particularly ugly in the typescript - there are
> ways around that, but better to have a plain text prompt.
>
> 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?

case $(ps -ocomm= "$PPID") in
          script) echo Inside script ;;
          *) echo Not a \"script\" shell ;;
esac

-- 
        Chris F.A. Johnson                      <http://cfaj.freeshell.org>
        ========= Do not reply to the From: address; use Reply-To: ========
        Author:
        Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
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