[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How can a script know whether it is directly called in a terminal?
From: |
Koichi Murase |
Subject: |
Re: How can a script know whether it is directly called in a terminal? |
Date: |
Wed, 25 Mar 2020 13:54:14 +0900 |
2020/3/23 2:47 Peng Yu
> I'd like to know within a script whether it is called directly by
> someone at the terminal.
>
> Or it is called by another script (whether that script is called from
> a terminal or not, it doesn't matter.) Is there a way to perform this
> test? Thanks.
I guess you want to test whether the script is directly called by an
interactive session of shells. If you want to check if the parent
process is the session leader or not, you can compare PPID with its
SID.
if [[ $(ps -o sid $PPID | tail -n +2) == $PPID ]]; then
echo This script is called by the session leader.
else
echo This script is called by another script.
fi
But this approach doesn't work if the interactive shell is not the
session leader. In that case, maybe you can analyze the command-line
arguments of the parent process, which can be obtained by e.g. $(ps -o
args $PPID | tail -n +2).
Best regards,
Koichi