Solved

Test for interactive vs non-interactive shell

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

19 Experts available now in Live!

Get 1:1 Help Now