删除数据库用户

#!/usr/bin/ksh
#################################
# 1、upload the shell command to the server
# 2、grant the 777 to the command
# 3、use method like as follow: ./dropuser.sh username
# the username as the parameter for the shell command
# and the username is not distinguish the upper and lowwer
##################################
echo "start delete link"
userName=$1;
userName=`echo $userName |tr "[a-z]" "[A-Z]"`;
querysessionnum=`sqlplus / as sysdba << EOF
Select count(*) from v\\$session where username='$userName';
Select sid,SERIAL# from v\\$session where username='$userName';
EOF`

droplink=`sqlplus / as sysdba << EOF
drop user $userName cascade;
EOF`

sessionnum=`echo $querysessionnum | awk -F'SQL>' '{print $2}' | awk -F' ---------- ' '{print $2}'`

echo "link number :"$sessionnum
if [ $sessionnum = 0 ]
then
    temp=`echo $droplink`
    exit
fi    
sessionsidser=`echo $querysessionnum | awk -F'SQL>' '{print $3}'`
i=2
while true
do
    sessionsids=`echo $sessionsidser | awk -F'SID SERIAL# ---------- ----------' '{print $'$i';}' | awk -F'rows selected.' '{print $1}'`
    if [ "x$sessionsids" = "x" ]
    then
        break;
    fi
    numindex=1
    while true
    do
        sessionsidone=`echo $sessionsids | awk -F' ' '{print $'$numindex';}'`
        if [ "x$sessionsidone" = "x" ]
        then
            break;
        fi
        let numindex=numindex+1
        sessionsidtwo=`echo $sessionsids | awk -F' ' '{print $'$numindex';}'`
        if [ "x$sessionsidtwo" = "x" ]
        then
            break;
        fi
        let numindex=numindex+1
        echo "alter system kill session  '$sessionsidone,$sessionsidtwo';" >> dellik.sql
    done
    let i=i+1    
done
sqlplus / as sysdba << EOF
    @dellik.sql
EOF
rm dellik.sql

temp=`echo $droplink`


你可能感兴趣的:(linux)