May 2007
Beginner
628 pages
15h 46m
English
You have some code you want to run only if you are (or are not) running interactively.
Use the following case
statement:
#!/usr/bin/env bash
# cookbook filename: interactive
case "$-" in
*i*) # Code for interactive shell here
;;
*) # Code for non-interactive shell here
;;
esac$- is a string listing of all the current shell option flags. It will
contain i if the shell is
interactive.
You may also see code like the following (this will work, but the solution above is the preferred method):
if [ "$PS1" ]; then
echo This shell is interactive
else
echo This shell is not interactive
fihelp case
help set
Branching Many Ways, for more explanation
of the case statement
Read now
Unlock full access