Solved

Test for interactive vs non-interactive shell

Posted on 2008-10-20
3
847 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…

685 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