Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how to test for interactive shell
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
drjimmy42
Guru
Guru


Joined: 03 Feb 2003
Posts: 512
Location: Nashua, NH

PostPosted: Mon Oct 13, 2003 2:54 pm    Post subject: how to test for interactive shell Reply with quote

Quick shell question. I want to write a script that behaves differently if called from a shell and when invoked by rox or nautilus or something.

My first thought was to test for whether or not the variable PS1 was null. If it was null then we were called from a file manager.

However, my PS1 variable has color in it, so it starts with an escape sequence. So when I test for it with -n it always comes back as zero length even if it is set.

Can someone tell me another way to test for interactive shells? Thanks.
Back to top
View user's profile Send private message
ozonator
Guru
Guru


Joined: 11 Jun 2003
Posts: 591
Location: Ontario, Canada

PostPosted: Mon Oct 13, 2003 7:02 pm    Post subject: Reply with quote

Bash does different things when starting as an interactive login shell (/etc/profile, ~/.profile, etc.), as a non-login but interactive shell (~/.bashrc), and non-interactively (checks $BASH_ENV) --- maybe you could use settings or variables in the appropriate one of those to do what you want (the $BASH_ENV option, at first glance, seems like it might be most useful to you). The bash man page has a description near the top concerning what gets read, when, and in what order.

Or, you could invoke bash with --noprofile or --norc, or even with --rcfile (i.e., put anything special in a separate script, and point bash to it with the --rcfile option). Which way might be best for you depends, I guess, on what exactly it is you want to be different between interactive and non-interactive invocations of bash.
Back to top
View user's profile Send private message
drjimmy42
Guru
Guru


Joined: 03 Feb 2003
Posts: 512
Location: Nashua, NH

PostPosted: Mon Oct 13, 2003 7:24 pm    Post subject: Reply with quote

I want a script that runs emacs in the present terminal (with the -nw option) if I call it from a terminal, but opens a new terminal and calls

Code:
exec Eterm -e emacs -nw


if it is called from a non interactive shell. Right now I just call emacs -nw. If it succeeds, great, if not, call the exec line. Its not great, but it works.
Back to top
View user's profile Send private message
Souperman
Guru
Guru


Joined: 14 Jul 2003
Posts: 449
Location: Cape Town, South Africa

PostPosted: Mon Oct 13, 2003 7:37 pm    Post subject: Reply with quote

bash (assuming you're using it -- pretty safe assumption I guess), has a special shell variable $-, which just contains a string of letters (flags) which describe how it is running. If $- contains the letter 'i', the shell is interactive.

Example:
Code:

case $- in
*i*)
        #do stuff for interactive operation
        echo "I am interactive"
;;
*)
        #do stuff for non-interactive operation
        echo "I am not interactive"
;;
esac

_________________
moo
Back to top
View user's profile Send private message
drjimmy42
Guru
Guru


Joined: 03 Feb 2003
Posts: 512
Location: Nashua, NH

PostPosted: Mon Oct 13, 2003 7:56 pm    Post subject: Reply with quote

gpd wrote:
bash (assuming you're using it -- pretty safe assumption I guess), has a special shell variable $-, which just contains a string of letters (flags) which describe how it is running. If $- contains the letter 'i', the shell is interactive.

Example:
Code:

case $- in
*i*)
        #do stuff for interactive operation
        echo "I am interactive"
;;
*)
        #do stuff for non-interactive operation
        echo "I am not interactive"
;;
esac


Unfortunately, testing that from inside a script doesn't work. when I call my script from the command line 'i' is not in $- when inside the script, even though it is there when I echo $- from the command line. Once its in the script, its not interactive anymore I guess. Nice try though. Thanks.
Back to top
View user's profile Send private message
Souperman
Guru
Guru


Joined: 14 Jul 2003
Posts: 449
Location: Cape Town, South Africa

PostPosted: Mon Oct 13, 2003 8:02 pm    Post subject: Reply with quote

OK, guess I misunderstood what you're trying to acheive.
_________________
moo
Back to top
View user's profile Send private message
drjimmy42
Guru
Guru


Joined: 03 Feb 2003
Posts: 512
Location: Nashua, NH

PostPosted: Mon Oct 13, 2003 8:16 pm    Post subject: Reply with quote

Code:
#!/bin/bash

#Take arguments from command line.
ARGS="$@"

#if there aren't any. then open CWD in dired
[ $# = 0 ] && ARGS="."
   
COMMAND="emacs -nw -bg black -fg lightgrey -q -l ~/.emacs.d/init/.emacs-minimal $ARGS"

$COMMAND || exec Eterm -e $COMMAND




This is my script. The whole thing. This is it. Its that last line that bugs me. It works ok now, it just seemed to me that there was a way to test for this, but my color prompt is stopping that.
Back to top
View user's profile Send private message
carpaski
Retired Dev
Retired Dev


Joined: 04 Aug 2002
Posts: 43
Location: Chicago, IL

PostPosted: Mon Oct 13, 2003 9:15 pm    Post subject: Reply with quote

tty -q << /dev/fd/1 && echo "Is a terminal."
_________________
Nicholas Jones <carpaski@gentoo.org>
Gentoo Linux Developer
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum