-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal-or-not.sh
executable file
·69 lines (55 loc) · 3.36 KB
/
terminal-or-not.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# This is a script to check if your script attached to in a terminal or not. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# in_terminal #
# -------------------------------------------------------------------------------- #
# Returns true if we are attached to a terminal or false if not. #
# -------------------------------------------------------------------------------- #
function in_terminal()
{
[[ -t 1 ]] && return 0 || return 1;
}
# -------------------------------------------------------------------------------- #
# in_pipe #
# -------------------------------------------------------------------------------- #
# Returns true if we are attached to a pipe or false if not. #
# -------------------------------------------------------------------------------- #
function in_pipe()
{
[[ -p /dev/stdout ]] && return 0 || return 1;
}
# -------------------------------------------------------------------------------- #
# in_redirection #
# -------------------------------------------------------------------------------- #
# Returns true if we are attached to a redirection or false if not. #
# -------------------------------------------------------------------------------- #
function in_redirection()
{
[[ ! -t 1 && ! -p /dev/stdout ]] && return 0 || return 1;
}
# -------------------------------------------------------------------------------- #
# Run Tests #
# -------------------------------------------------------------------------------- #
# A VERY simple test function to ensure that it all works #
# -------------------------------------------------------------------------------- #
function run_test()
{
in_terminal && echo 'STDOUT is attached to TTY'
in_pipe && echo 'STDOUT is attached to a pipe'
in_redirection && echo 'STDOUT is attached to a redirection'
}
# -------------------------------------------------------------------------------- #
# Main() #
# -------------------------------------------------------------------------------- #
# This is the actual 'script' and the functions/sub routines are called in order. #
# -------------------------------------------------------------------------------- #
run_test
# -------------------------------------------------------------------------------- #
# End of Script #
# -------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# -------------------------------------------------------------------------------- #