Microsoft
Software
Hardware
Network
Question : Test for interactive vs non-interactive shell
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
Answer : Test for interactive vs non-interactive shell
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
Random Solutions
Scrolling Marque
How to convert a raster image into a vector image?
Retrieving n rows from sybase database - pagination
Free/Busy Update Settings
Windows 98 Shortcuts disappear after restart
Add individual members to Query-Based Distribution Group?
Integrating online orderform info. with Microsoft Access based database
Scan text for a specific word and print surrounding text
Interview questions for the position of Online line marketing manager
move exchange database to another drive