1832856 Members
2999 Online
110047 Solutions
New Discussion

Re: Interactive or not

 
PJJ
Frequent Advisor

Interactive or not

Hi all,
I need to change my DISPLAY settings, so I try to figure out if my script runs interactive or from cron. Therefore I'm trying to run the script example from thread 63219, but it gives errors.
Any help is appreciated!

#!/usr/bin/sh
[ -t 0 ] && print -u2 "stdin is interactive"
[ -t 1 ] && print -u2 "stdout is interactive"
[ -t 0 ] || print -u2 "stdin is NOT interactive"
[ -t 1 ] || print -u2 "stdout is NOT interactive"
exit 0

./my.sh
./my.sh[2]: Syntax error at line 2 : `&' is not expected.
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Interactive or not

Hi:

Simply test this way:

if [ -t 0 ]; then
echo "I'm running interactively"
else
echo "I am not associated with a tty!"
fi

Regards!

...JRF...
spex
Honored Contributor

Re: Interactive or not

Schrikbeeld,

Did you actually copy and paste the script containing "[" and "]", or did you substitute "[" and "]" (left and right brackets) for these character codes?

The script should read:

#!/usr/bin/sh
[ -t 0 ] && print -u2 "stdin is interactive"
[ -t 1 ] && print -u2 "stdout is interactive"
[ -t 0 ] || print -u2 "stdin is NOT interactive"
[ -t 1 ] || print -u2 "stdout is NOT interactive"
exit 0

PCS
PJJ
Frequent Advisor

Re: Interactive or not

Then it returns
I am not associated with a tty!
PJJ
Frequent Advisor

Re: Interactive or not

Spex, I litteraly copy/pasted the &#... syntax.
spex
Honored Contributor

Re: Interactive or not

Since there is no tty, it is running non-interactively.
spex
Honored Contributor

Re: Interactive or not

So in your script,

if [ -t 0 ]; then
# script running from command line



else
# script run by cron



fi
exit 0

PCS
PJJ
Frequent Advisor

Re: Interactive or not

Ok, as i understand the &# syntax is to be replaced by [ and ] respectively.
I'm getting it. Thanks for the quick replies!