Solved

Test for interactive vs non-interactive shell

Posted on 2008-10-20
3
856 Views
Last Modified: 2013-12-26
I am trying to incorporate a test into my bash scripts which will select different code depending on whether or not it runs in an interactive or a non-interactive mode.

Given the following two programs:

File: qk.sh
---------------
#!/bin/bash

plen=${#PS1}
if [ -z "$PS1" ]; then
        echo -e "This shell is interactive:$plen :$PS1"
else
        echo -e "This shell is non-interactive:$plen :$PS1"
fi


File2: qk2.sh
-------------
#!/bin/bash
./qk.sh > ckit
cat ./ckit



When I run qk.sh during and interactive session, it prints

"This shell is interactive:0 : "

When I run qk2.sh, which I assume means I am running qk.sh non-interactively, I get the same output.

Any insights would be appreciated.

-G



if [ -z "$PS1" ]; then
        echo This shell is not interactive
else
        echo This shell is interactive
fi
0
Comment
Question by:gmanpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 22760692
if ps -p $PPID | grep -- '-[a-z]*$' ; then
  echo -e "This shell is interactive:$plen :$PS1"
else
          echo -e "This shell is non-interactive:$plen :$PS1"
fi
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 22760762
if perl -e 'exit -t STDOUT' ; then
        echo -e "This shell is non-interactive:$plen :$PS1"
else
        echo -e "This shell is interactive:$plen :$PS1"
fi

0
 

Author Closing Comment

by:gmanpert
ID: 31507915
The first method did not work for me. The output was "This shell is non-interactive:0 : " when I ran it from the bash command line.  

The second one works wonders. - Thanks.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question