Operating System - HP-UX
1833207 Members
3202 Online
110051 Solutions
New Discussion

Re: Determining Where Script Was Started From

 
curt larson_1
Honored Contributor

Re: Determining Where Script Was Started From

Mr Stephenson and Borowski have your answer

a login shell has a leading dash

in your profile

case $0 in
-*) loginshell=true;;
*) loginshell=false;;
esac

and if interactive

case $- in
*i*) interactiveshell=true;;
*) interactiveshell=false;;
esac

and if attached to a terminal

if [[ -t 0 ]] ;then
terminal=true
else
terminal=false
fi
curt larson_1
Honored Contributor

Re: Determining Where Script Was Started From

However, we login via CDE and have hpterm/dtterm windows opened - they do not behave this way.

the standard installation of CDE doesn't source /etc/profile. So, the only way you'd be in /etc/profile is if one of your scripts was sourceing it, i.e. $0 would not be -sh.

The above is true unless you've customized the CDE login to source /etc/profile somewhere. And, it shouldn't be too hard to set something up to recognize your sourceing /etc/profile during the CDE startup.
Volker Borowski
Honored Contributor

Re: Determining Where Script Was Started From

Pete,

what is the desired behavior when logging into CDE ?
Get /etc/copyright for each hpterm that is opened or only for the very first one ?

Volker
Pete Randall
Outstanding Contributor

Re: Determining Where Script Was Started From

Curt,

You're on the right track - we force /etc/profile to be sourced in .dtprofile. I could recognize this and cat /etc/copyright only then, but that leads to Volker's question:


Volker,

My preferred behaviour would be to have the copyright display for each hpterm opened.



Pete

Pete
Patrick Wallek
Honored Contributor

Re: Determining Where Script Was Started From

Pete,

How about this:

PP1=$(ps -fp $PPID | grep -v PID | awk '{print $3}')
PP2=$(ps -p $PP1 | grep -v PID | awk '{print $4}')
if [ "${PP2}" = "hpterm" ] ; then
cat /etc/copyright
fi

PP1 will get the PPID of the current process, which should be the shell, and then PP2 gets the PPID of the shell, which should be hpterm. Soo if PP2 = hpterm then show /etc/copyright. You could do the same thing for dtterm as well. I think this should work for you.
curt larson_1
Honored Contributor

Re: Determining Where Script Was Started From

I think i've spent too much time looking at that copyright already. So, I put this in my /etc/profile

echo "\nThe copyright notice on this machine ($(hostname)) can read"
echo "by typing: cat /etc/copyright"

so I don't have to look at it anymore then I want to. Of course, when using CDE you get a big screen full of it (unless you customize that also). So, you don't have to show it again for every terminal window started.
curt larson_1
Honored Contributor

Re: Determining Where Script Was Started From

And I only print my copyright message if the shell is interactive, see above post about testing if the shell is interactive. Cron jobs shouldn't be interactive (I haven't tested it) and you would think that each hpterm would be.
curt larson_1
Honored Contributor

Re: Determining Where Script Was Started From

I'm not too sure about this, so if i'm incorrect be sure to correct me. but, I think when using CDE your login environment is sourced only once at login. And, when ever you start another terminal your just given this same login environment. your profile isn't going to be re-sourced.

you can put something like
*loginShell: true

in the ~/.Xdefaults file or approriate cde configuration file. This will make each hpterm/dtterm/xterm's shell a login shell. But, /etc/profile still isn't sourced just the $HOME/.profile is.

So, if you wanted each hpterm to print a copyright. I think you could do by setting the above resource, then in $HOME/.profile have the copyright printed if the shell is interactive.
Pete Randall
Outstanding Contributor

Re: Determining Where Script Was Started From

I've solved it - to my satisfaction anyway.

Any script, batch or cron, invokes another script, we'll call it envscript, that was supposed to set all the required environment variables (this just in case it was cron - overkill otherwise). We also had some menuing applications that had there own variable setups. And, of course, we had /etc/profile that was handling the CDE logins (because we forced it to be read via .dtprofile).

I wanted to consolidate all this and just have one source for environment variables: /etc/profile.

The answer in this case was to add a variable in envscript and then test it in /etc/profile. I aplogize for torturing everyone when the solution was so simple. I just couldn't see it (as you could tell from my first attempt to state the problem). I was trying to make it more complicated than it needed to be.

Thanks all,


Pete

Pete
RAC_1
Honored Contributor

Re: Determining Where Script Was Started From

I just thought of this. checking variables in script against vars in /etc/profile.

But this long thread just got me off. Time to leave.

Anil
There is no substitute to HARDWORK