-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Actually there are three states I want to base some conditional execution on.
I can do this now based on feeding the script a command-line argument. But I'm wanting to have the script itself determine if:
1) It's running in a VT such as found in runlevel 3 or via ctrl+alt+F1 in runlevel 5.
2) It's running in an "xterm", "konsole", "aterm", or (ANY other x based terminal window).
3) It's not attached to a terminal or VT (such as might happen if the script was called from alt+F2 run prompt).
How can a bash script test it's environment for these three different conditions???
Please! and Thank you!
- -- | ? ? | | -=- -=- I'm NOT clueless... | <?> <?> But I just don't know. | ^ Joe (theWordy) Philbrook | --- J(tWdy)P | jtwdyp@ttlc.net | ? ?
############################################################## # You can find my public gpg key at http://pgpkeys.mit.edu/ # ##############################################################
On Wednesday, Jul 7th 2004 at 12:51 -0400, quoth Joe(theWordy)Philbrook:
=>-----BEGIN PGP SIGNED MESSAGE----- =>Hash: SHA1 => => =>Actually there are three states I want to base some conditional execution =>on. => =>I can do this now based on feeding the script a command-line argument. =>But I'm wanting to have the script itself determine if: => =>1) It's running in a VT such as found in runlevel 3 or via ctrl+alt+F1 => in runlevel 5. => =>2) It's running in an "xterm", "konsole", "aterm", or (ANY other x based => terminal window). => =>3) It's not attached to a terminal or VT (such as might happen if the => script was called from alt+F2 run prompt). => =>How can a bash script test it's environment for these three different =>conditions??? => =>Please! and Thank you!
1. Check out the tty command and the runlevel command.
2. Look at the TERM environment variable.
if [[ "$TERM" = xterm ]] ...
3. If the PS1 environment variable is null then you were not called interactively. e.g. if [[ -z "$PS1" ]] then echo 'I am not interactive' else echo 'I am interactive.' fi
On Wed, 2004-07-07 at 11:51, Joe(theWordy)Philbrook wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Actually there are three states I want to base some conditional execution on.
I can do this now based on feeding the script a command-line argument. But I'm wanting to have the script itself determine if:
It's running in a VT such as found in runlevel 3 or via ctrl+alt+F1 in runlevel 5.
It's running in an "xterm", "konsole", "aterm", or (ANY other x based terminal window).
It's not attached to a terminal or VT (such as might happen if the script was called from alt+F2 run prompt).
How can a bash script test it's environment for these three different conditions???
Please! and Thank you!
I have not tried what you want, Someone else may be able to give a complete answer.
However, when I have a similar question, a very good and handy reference to locate the answers for most and to lead me in the right direction in shell scripting is the book /UNIX shell programming/ by Lowell Jar Arthur and Ted Burns.
If I am considering Perl instead of the shell I use the /Perl Cookbook/ and /Programming Perl/ among others to get the details as to how/why it works that way.
With these and a little research I find most things I need for anything I want to do. Over time even the difficult task becomes easier as the techniques are learned.
YMMV Jeff
On Wed, 7 Jul 2004, Jeff Vian wrote:
I have not tried what you want, Someone else may be able to give a complete answer.
However, when I have a similar question, a very good and handy reference to locate the answers for most and to lead me in the right direction in shell scripting is the book /UNIX shell programming/ by Lowell Jar Arthur and Ted Burns.
if you just want example scripts, i recommend cameron simpson's script repository, at http://www.cskk.ezoshosting.com/cs/css.
and www.moongroup.com hosts a mailing list devoted to scripting.
rday
On Wed, Jul 07, 2004 at 12:51:05PM -0400, Joe(theWordy)Philbrook wrote:
Actually there are three states I want to base some conditional execution on.
I can do this now based on feeding the script a command-line argument. But I'm wanting to have the script itself determine if:
- It's running in a VT such as found in runlevel 3 or via ctrl+alt+F1 in runlevel 5.
The tty command should work here. The ctrl+alt+F1 group look like:
# tty /dev/tty1
tty2, tty3, tty4... etc.
- It's running in an "xterm", "konsole", "aterm", or (ANY other x based terminal window).
For normal xterm, konsole you will see /dev/pts/nn for these and ssh login:
$ tty /dev/pts/26
It is possible to see something different if you have old ptys configured (takes work). The openpty() getpty() call will return one of the standard tty's. Even inside of emacs I see $ tty /dev/pts/28
- It's not attached to a terminal or VT (such as might happen if the script was called from alt+F2 run prompt).
See /etc/bashrc # are we an interactive shell? if [ "$PS1" ]; then case $TERM in xterm*) ....etc.
The run level can be tested by looking at the output of "who -r: or "runlevel".
You may also have other valuable hooks in the environment. see "set", "printenv", "env". Look for stuff like. SSH_TTY=/dev/pts/27 TERM=xterm DISPLAY=:0.0 SHELL=/bin/bash COLORTERM=gnome-terminal
It is possible for users to set some of these in unexpected ways so it is worth testing and not trusting environment variables for security purposes.
For example you can explore the environment given a process when a script is called from alt+F2 run prompt by having a script do something as simple as "env > /tmp/show-my-env"
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Jul 7 Steven W. Orr (3098) Re: [OT] how can a bash script determin Jul 7 Jeff Vian (3145) Re: [OT] how can a bash script determin Jul 7 Robert P. J. Day (2103) Re: [OT] how can a bash script determin Jul 8 Nifty Hat Mitch (3759) Re: [OT] how can a bash script determin
Thanks guys, I appreciate the help. Actually though Somebody sent me an test script off list that does a good job of demonstrating how a bash script can check for all three conditions, AND points at the wealth of info that can be extracted with env. I wanted to offer a big Thank you but I seam to have lost the e-mail... Sigh!
I know I had it, as I copied the script from it. And I don't remember deleting that E-mail message but I can't find it.
So to you who sent me a script something like this. (you know who you are...) THANKS!
#!/bin/bash
if ! tty -s then echo "Running non-interactive.." > ~/wai elif test ! -z $DISPLAY then echo "Running in GUI" > ~/wai else echo "Running on console" > ~/wai fi echo '___________' >> ~/wai env >> ~/wai
- -- | --- ___ | <0> <-> Joe (theWordy) Philbrook | ^ J(tWdy)P | ~___/~ <jtwdyp@ttlc.net>
############################################################## # You can find my public gpg key at http://pgpkeys.mit.edu/ # ##############################################################