1834926 Members
2535 Online
110071 Solutions
New Discussion

Re: shell script test

 
Mark F Johnson
New Member

shell script test

How do you test if a shell script is executing interactively, from a terminal, or from cron?
like the monkey said when his tail got ran over by the lawn mower... It won't be long now!
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor

Re: shell script test

Hi Mark:

[ -t -eq 0 ] will return true (exit status = 0) if the shell is interactive.

...JRF...

Dan Hetzel
Honored Contributor

Re: shell script test

Hi Mark,

#!/usr/bin/sh
if [ tty >/dev/null 2>&1 ]
then
echo "terminal"
else
echo "not a terminal"
fi

'tty' used alone prints either the terminal name or an error message.Redirecting standard output and standard error removes all messages.

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com