Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

k shell function that checks to see if commands are available and more

Status
Not open for further replies.

RobJordan

MIS
Apr 3, 2001
296
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top