Scripting : set variables depending how the where launched (terminal mode, cron/auto mode, ...)
You all know the deal : when you launch a command it (obviously) comes with all your environment variables. But when you want to cron it, none of them is present. We hence want to set some variables, but only in some cases. The "tty" command will help us do so.
You can use the tty tool to check if a script is called from the standard output :
if ! tty -s
then
exec >/dev/null 2>&1
else
MAIL_DEST="email@server.ext"
fi
User Commands tty(1)
NAME tty - return user's terminal name
SYNOPSIS tty [-l] [-s]
DESCRIPTION The tty utility writes to the standard output the name of
the terminal that is open as standard input. The name that
is used is equivalent to the string that would be returned
by the ttyname(3C) function.
OPTIONS The following options are supported:
-l Prints the synchronous line number to which the
user's terminal is connected, if it is on an active
synchronous line.
-s Inhibits printing of the terminal path name, allow-
ing one to test just the exit status.
EXIT STATUS
The following exit values are returned:
0 Standard input is a terminal.
1 Standard input is not a terminal.
>1 An error occurred.
You can use the tty tool to check if a script is called from the standard output :
if ! tty -s
then
exec >/dev/null 2>&1
else
MAIL_DEST="email@server.ext"
fi
- The command tty returns :
User Commands tty(1)
NAME tty - return user's terminal name
SYNOPSIS tty [-l] [-s]
DESCRIPTION The tty utility writes to the standard output the name of
the terminal that is open as standard input. The name that
is used is equivalent to the string that would be returned
by the ttyname(3C) function.
OPTIONS The following options are supported:
-l Prints the synchronous line number to which the
user's terminal is connected, if it is on an active
synchronous line.
-s Inhibits printing of the terminal path name, allow-
ing one to test just the exit status.
EXIT STATUS
The following exit values are returned:
0 Standard input is a terminal.
1 Standard input is not a terminal.
>1 An error occurred.