Solved

Test for interactive vs non-interactive shell

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…

710 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