Solved

Test for interactive vs non-interactive shell

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now