-
Notifications
You must be signed in to change notification settings - Fork 585
/
047-validator.sh
executable file
·88 lines (70 loc) · 2.24 KB
/
047-validator.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
#!/bin/sh
# VALIDATOR - Checks to ensure that all environment variables are valid
# looks at SHELL, HOME, PATH, EDITOR, MAIL, and PAGER
errors=0
in_path()
{
# given a command and the PATH, try to find the command. Returns
# 1 if found, 0 if not. Note that this temporarily modifies the
# the IFS input field seperator, but restores it upon completion.
cmd=$1 path=$2 retval=0
oldIFS=$IFS; IFS=":"
for directory in $path
do
if [ -x $directory/$cmd ] ; then
retval=1 # if we're here, we found $cmd in $directory
fi
done
IFS=$oldIFS
return $retval
}
validate()
{
varname=$1 varvalue=$2
if [ ! -z $varvalue ] ; then
if [ "${varvalue%${varvalue#?}}" = "/" ] ; then
if [ ! -x $varvalue ] ; then
echo "** $varname set to $varvalue, but I cannot find executable."
errors=$(( $errors + 1 ))
fi
else
if in_path $varvalue $PATH ; then
echo "** $varname set to $varvalue, but I cannot find it in PATH."
errors=$(( $errors + 1 ))
fi
fi
fi
}
####### Beginning of actual shell script #######
if [ ! -x ${SHELL:?"Cannot proceed without SHELL being defined."} ] ; then
echo "** SHELL set to $SHELL, but I cannot find that executable."
errors=$(( $errors + 1 ))
fi
if [ ! -d ${HOME:?"You need to have your HOME set to your home directory"} ]
then
echo "** HOME set to $HOME, but it's not a directory."
errors=$(( $errors + 1 ))
fi
# Our first interesting test: are all the paths in PATH valid?
oldIFS=$IFS; IFS=":" # IFS is the field separator. We'll change to ':'
for directory in $PATH
do
if [ ! -d $directory ] ; then
echo "** PATH contains invalid directory $directory"
errors=$(( $errors + 1 ))
fi
done
IFS=$oldIFS # restore value for rest of script
# The following can be undefined, and they can also be a progname, rather
# than a fully qualified path. Add additional variables as necessary for
# your site and user community.
validate "EDITOR" $EDITOR
validate "MAILER" $MAILER
validate "PAGER" $PAGER
# and, finally, a different ending depending on whether errors > 0
if [ $errors -gt 0 ] ; then
echo "Errors encountered. Please notify sysadmin for help."
else
echo "Your environment checks out fine."
fi
exit 0