-
Notifications
You must be signed in to change notification settings - Fork 11
/
is.sh
executable file
路132 lines (122 loc) 路 3.55 KB
/
is.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
#
# Copyright (c) 2016 J贸zef Soko艂owski
# Distributed under the MIT License
#
# For most current version checkout repository:
# https://github.com/qzb/is.sh
#
is() {
if [ "$1" == "--help" ]; then
cat << EOF
Conditions:
is equal VALUE_A VALUE_B
is matching REGEXP VALUE
is substring VALUE_A VALUE_B
is empty VALUE
is number VALUE
is gt NUMBER_A NUMBER_B
is lt NUMBER_A NUMBER_B
is ge NUMBER_A NUMBER_B
is le NUMBER_A NUMBER_B
is file PATH
is dir PATH
is link PATH
is existing PATH
is readable PATH
is writeable PATH
is executable PATH
is available COMMAND
is older PATH_A PATH_B
is newer PATH_A PATH_B
is true VALUE
is false VALUE
Negation:
is not equal VALUE_A VALUE_B
Optional article:
is not a number VALUE
is an existing PATH
is the file PATH
EOF
exit
fi
if [ "$1" == "--version" ]; then
echo "is.sh 1.1.0"
exit
fi
local condition="$1"
local value_a="$2"
local value_b="$3"
if [ "$condition" == "not" ]; then
shift 1
! is "${@}"
return $?
fi
if [ "$condition" == "a" ] || [ "$condition" == "an" ] || [ "$condition" == "the" ]; then
shift 1
is "${@}"
return $?
fi
case "$condition" in
file)
[ -f "$value_a" ]; return $?;;
dir|directory)
[ -d "$value_a" ]; return $?;;
link|symlink)
[ -L "$value_a" ]; return $?;;
existent|existing|exist|exists)
[ -e "$value_a" ]; return $?;;
readable)
[ -r "$value_a" ]; return $?;;
writeable)
[ -w "$value_a" ]; return $?;;
executable)
[ -x "$value_a" ]; return $?;;
available|installed)
which "$value_a"; return $?;;
empty)
[ -z "$value_a" ]; return $?;;
number)
echo "$value_a" | grep -E '^[0-9]+(\.[0-9]+)?$'; return $?;;
older)
[ "$value_a" -ot "$value_b" ]; return $?;;
newer)
[ "$value_a" -nt "$value_b" ]; return $?;;
gt)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a > $value_b ? 0 : 1}"; return $?;;
lt)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a < $value_b ? 0 : 1}"; return $?;;
ge)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a >= $value_b ? 0 : 1}"; return $?;;
le)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a <= $value_b ? 0 : 1}"; return $?;;
eq|equal)
[ "$value_a" = "$value_b" ] && return 0;
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a == $value_b ? 0 : 1}"; return $?;;
match|matching)
echo "$value_b" | grep -xE "$value_a"; return $?;;
substr|substring)
echo "$value_b" | grep -F "$value_a"; return $?;;
true)
[ "$value_a" == true ] || [ "$value_a" == 0 ]; return $?;;
false)
[ "$value_a" != true ] && [ "$value_a" != 0 ]; return $?;;
esac > /dev/null
return 1
}
if is not equal "${BASH_SOURCE[0]}" "$0"; then
export -f is
else
is "${@}"
exit $?
fi