This function can be pasted into almost any script
to see if commands are available to your script.
It allows you to test if a file or directory is
readalbe, readable and writable or readable and executable.
This script may work in other shells,
but has only been tested in the k shell.
#!/usr/bin/ksh
Validate () # validate that commands are available for script to use
# Validate $1 r (test if file is readable)
# Validate $1 rw (test if file is readable and writable)
# Validate $1 rx (test if file is readable and executable)
# $1 is file or command to test, $2 can be r, rw or rx
# ex: Validate /usr/bin/ls rx
{
case $2 in
r)
if test -a $1
then
echo "$1 found"
else
echo "$1 not found"
which $1
fi
if test -r $1
then
echo "$1 readable"
else
echo "$1 is not readable"
which $1
fi
;;
rw)
if test -a $1
then
echo "$1 found"
else
echo "$1 not found"
which $1
fi
if test -w $1
then
echo "$1 writable"
else
echo "$1 is not writable"
fi
;;
rx)
if test -a $1
then
echo "$1 found"
else
echo "$1 not found"
which $1
fi
if test -x $1
then
echo "$1 executable"
else
echo "$1 is not executable"
fi
;;
*)
echo "Improper use of Validate function!"
echo "Please view function comments"
exit 1
;;
esac
}
#Demonstration of the function
Validate /tmp r
Validate /usr/bin/echo rx
Validate /usr/bin/ls rx
Validate /usr/bin/find rx
Validate /usr/bin/timex rx
Validate /usr/bin/whoami rx
Validate /usr/bin/pwd rx
Validate /usr/bin/kill rx
Validate /usr/bin/grep rx
Validate /usr/bin/cat rx
Validate /usr/bin/expr rx
Validate /usr/bin/printf rx
Validate /usr/bin/which rx
Robert G. Jordan
Robert@JORDAN2000.com
Unix Admin, United Airlines
to see if commands are available to your script.
It allows you to test if a file or directory is
readalbe, readable and writable or readable and executable.
This script may work in other shells,
but has only been tested in the k shell.
#!/usr/bin/ksh
Validate () # validate that commands are available for script to use
# Validate $1 r (test if file is readable)
# Validate $1 rw (test if file is readable and writable)
# Validate $1 rx (test if file is readable and executable)
# $1 is file or command to test, $2 can be r, rw or rx
# ex: Validate /usr/bin/ls rx
{
case $2 in
r)
if test -a $1
then
echo "$1 found"
else
echo "$1 not found"
which $1
fi
if test -r $1
then
echo "$1 readable"
else
echo "$1 is not readable"
which $1
fi
;;
rw)
if test -a $1
then
echo "$1 found"
else
echo "$1 not found"
which $1
fi
if test -w $1
then
echo "$1 writable"
else
echo "$1 is not writable"
fi
;;
rx)
if test -a $1
then
echo "$1 found"
else
echo "$1 not found"
which $1
fi
if test -x $1
then
echo "$1 executable"
else
echo "$1 is not executable"
fi
;;
*)
echo "Improper use of Validate function!"
echo "Please view function comments"
exit 1
;;
esac
}
#Demonstration of the function
Validate /tmp r
Validate /usr/bin/echo rx
Validate /usr/bin/ls rx
Validate /usr/bin/find rx
Validate /usr/bin/timex rx
Validate /usr/bin/whoami rx
Validate /usr/bin/pwd rx
Validate /usr/bin/kill rx
Validate /usr/bin/grep rx
Validate /usr/bin/cat rx
Validate /usr/bin/expr rx
Validate /usr/bin/printf rx
Validate /usr/bin/which rx
Robert G. Jordan
Robert@JORDAN2000.com
Unix Admin, United Airlines