I only want to determine from my POSIX shell script, if it is running interactively, but for some reason, the following function:
running_interactively()
{
printf '%s' ${-} | grep -F i > /dev/null 2>&1
}
returns false even if I run the script in terminal. Am I doing it wrong, or is the definition of interactive script somehow different from my plain idea of running the script by a user in terminal?
Snippet of the code:
#!/bin/sh
set -u
running_interactively()
{
# echo $- returns only u
printf '%s' ${-} | grep i > /dev/null 2>&1
}
print_error_and_exit()
{
# redirect all output from this function to standard error stream
if running_interactively
then
exec >&2
else
echo wrong again, smart ass
fi
...
}
print_error_and_exit someArgs