Change Owner, Korn Shell
#! /bin/ksh
# change_owner - hunt down files owned by a user and change the
# ownership to some other user.
# program variables
PROGRAM=${0##*/}
DIRECTORY=`pwd`
LISTMODE=0
OLDUSER="`id`"
OLDUSER="${OLDUSER%%\)*}"
OLDUSER="${OLDUSER##*\(}"
CURRENTUSER=${OLDUSER}
# bell - function to make a noise
function bell { echo "\c" ; }
# usage - function to display the command syntax
usage()
{
bell
echo "\nusage: ${PROGRAM} [-o olduser] [-l] [-d directory] newuser\n"
echo "flags: -o old user name or id, defaults to the current user."
echo " -l list the candidates only, make no changes."
echo " -d base directory, defaults to the current directory.\n"
exit 1
}
# process command line options
while getopts o:ld: option
do
case ${option}
in
o) OLDUSER=${OPTARG};;
l) LISTMODE=1;;
d) DIRECTORY=${OPTARG};;
\?) usage;;
esac
done
# make sure that the newuser is entered
if [ "${OPTIND}" -gt "$#" ]
then
echo "\nERROR: Missing the new user name or id."
usage
else
shift `expr ${OPTIND} - 1`
NEWUSER=${1}
fi
#
# validate the old user
grep "^${OLDUSER}:" /etc/passwd >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "\nERROR: ${OLDUSER} is not a valid user on this system."
usage
fi
# validate the new user
grep "^${NEWUSER}:" /etc/passwd >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "\nERROR: ${NEWUSER} is not a valid user on this system."
usage
fi
# validate permission to change ownership. The current user must
# be the old user, or root must be the current user.
if [ ! \( \( "${CURRENTUSER}" = "${OLDUSER}" \) -o \( "${CURRENTUSER}" = "root" \) \) ]
then
echo "\nERROR: You are not authorized to change the ownership"
echo " of files owned by ${OLDUSER}."
usage
fi
# validate the directory
if [ ! -d "${DIRECTORY}" ]
then
echo "\nERROR: ${DIRECTORY} is not a directory."
usage
fi
# execute the command to change the ownership or list candidates.
if [ "${LISTMODE}" -eq 1 ]
then
find ${DIRECTORY} -ls 2>/dev/null | nawk '{
if (NF > 9) {
if ($5 == username) {
printf "%s\n", $NF
}
}
}' username=${OLDUSER} - | ${PAGER:-more}
else
find ${DIRECTORY} -ls 2>/dev/null | nawk '{
if (NF > 9) {
if ($5 == username) {
printf "%s\n", $NF
}
}
}' username=${OLDUSER} - | xargs chown ${NEWUSER}
fi
exit 0
# change_owner - hunt down files owned by a user and change the
# ownership to some other user.
# program variables
PROGRAM=${0##*/}
DIRECTORY=`pwd`
LISTMODE=0
OLDUSER="`id`"
OLDUSER="${OLDUSER%%\)*}"
OLDUSER="${OLDUSER##*\(}"
CURRENTUSER=${OLDUSER}
# bell - function to make a noise
function bell { echo "\c" ; }
# usage - function to display the command syntax
usage()
{
bell
echo "\nusage: ${PROGRAM} [-o olduser] [-l] [-d directory] newuser\n"
echo "flags: -o old user name or id, defaults to the current user."
echo " -l list the candidates only, make no changes."
echo " -d base directory, defaults to the current directory.\n"
exit 1
}
# process command line options
while getopts o:ld: option
do
case ${option}
in
o) OLDUSER=${OPTARG};;
l) LISTMODE=1;;
d) DIRECTORY=${OPTARG};;
\?) usage;;
esac
done
# make sure that the newuser is entered
if [ "${OPTIND}" -gt "$#" ]
then
echo "\nERROR: Missing the new user name or id."
usage
else
shift `expr ${OPTIND} - 1`
NEWUSER=${1}
fi
#
# validate the old user
grep "^${OLDUSER}:" /etc/passwd >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "\nERROR: ${OLDUSER} is not a valid user on this system."
usage
fi
# validate the new user
grep "^${NEWUSER}:" /etc/passwd >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "\nERROR: ${NEWUSER} is not a valid user on this system."
usage
fi
# validate permission to change ownership. The current user must
# be the old user, or root must be the current user.
if [ ! \( \( "${CURRENTUSER}" = "${OLDUSER}" \) -o \( "${CURRENTUSER}" = "root" \) \) ]
then
echo "\nERROR: You are not authorized to change the ownership"
echo " of files owned by ${OLDUSER}."
usage
fi
# validate the directory
if [ ! -d "${DIRECTORY}" ]
then
echo "\nERROR: ${DIRECTORY} is not a directory."
usage
fi
# execute the command to change the ownership or list candidates.
if [ "${LISTMODE}" -eq 1 ]
then
find ${DIRECTORY} -ls 2>/dev/null | nawk '{
if (NF > 9) {
if ($5 == username) {
printf "%s\n", $NF
}
}
}' username=${OLDUSER} - | ${PAGER:-more}
else
find ${DIRECTORY} -ls 2>/dev/null | nawk '{
if (NF > 9) {
if ($5 == username) {
printf "%s\n", $NF
}
}
}' username=${OLDUSER} - | xargs chown ${NEWUSER}
fi
exit 0
Plone and its visual design is Copyright © 2000-