Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell how script was invoked

Status
Not open for further replies.

kbsinc

Programmer
Oct 26, 2002
2
US

Hi, is there any way I can tell how a script was started?
To explain further. I have written a 'wrapper' shell script
that starts numerous other existing utility scripts. I want the called 'utility scripts' to act slightly differently depending on how they were started. I need to determine if the 'utility script' was started by the 'wrapper script' or if it was just started via someone invoking it from their shell via the command line. The 'wrapper script' is also invoked via the shell command line.

Currently, I pass in an extra arg to the 'utility scripts' when started via the 'wrapper script' and use this in the 'utility script' to determine how it was started. I wonder if there is a better way. Thank you...
 
In ksh you can examine the result of
Code:
 ps -fp $PPID

Hope This Help
PH.
 
On Solaris the [tt]ptree[/tt] command will show the whole process tree.
[tt]
$ ptree $$
144 /usr/sbin/inetd -s
22833 in.telnetd
22836 -ksh
25889 ptree 22836
$
[/tt]
Maybe use this to determine who and how it was started.

PHV's solution is more to the point though.

Hope this helps.

 
There are a number of ways, but how inventive are your users likely to be at getting round them?

Here are a couple of thoughts
1. bash (and other shells) have a $SHLVL variable which increments with each sub-shell invoked. In the example, the script the user invokes has a value of 2, and the script which is invoked by a script has the value 3.

2. By using symbolic links, you can manipulate the value of $0 (the program name) inside a script. The invoked script gets the name of the symbolic link (foo calls baz, so bar ends up printing baz).

Example
Code:
$ ls -l
-rwxrw-r--     50 Nov 18 13:43 bar
lrwxrwxrwx      3 Nov 18 13:42 baz -> bar
-rwxrw-r--     57 Nov 18 13:43 foo

$ cat foo bar
#!/bin/bash
echo "arg ="$0
echo "foo level="$SHLVL
./baz

#!/bin/bash
echo "arg="$0
echo "bar level="$SHLVL

$ ./foo
arg =./foo
foo level=2
arg=./baz
bar level=3

$ ./bar
arg=./bar
bar level=2

--
 
I am running under ksh and the 'ps -fp $PPID' will meet my requirements for now. Thanks all for your input....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top