#! /bin/bash
read -p "请输入管理组名称" dba
read -p "请输入安装组名称" oinstall
read -p "请输入用户名称" tester
read -p "请输入用户密码" pass
# 定义editenv函数
editenv(){
cd /home/$tester
echo "ORACLE_BASE=/oracle" >> /home/$tester/.bash_profile
echo "ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1" >> /home/$tester/.bash_profile
echo "ORACLE_SID=orcl" >> /home/$tester/.bash_profile
echo "PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin" >> /home/$tester/.bash_profile
echo "LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib" >> /home/$tester/.bash_profile
echo "export ORACLE_BASE" >> /home/$tester/.bash_profile
echo "export ORACLE_HOME" >> /home/$tester/.bash_profile
echo "export ORACLE_SID" >> /home/$tester/.bash_profile
echo "export PATH" >> /home/$tester/.bash_profile
echo "export LD_LIBRARY_PATH" >> /home/$tester/.bash_profile
source /home/$tester/.bash_profile
if [ `echo $ORACLE_BASE`="/oracle" ]
then
echo "配置文件更新成功!"
else
echo "配置文件更失败!"
fi
}
#定义创建目录函数
createdir(){
mydir="/oracle/product/10.2.0/db_1"
mkdir -p $mydir 1>> /tmp/correct.txt 2>> error.txt
if [ -d "$mydir" ]
then
echo "目录创建成功"
# 修改 oracle 安装目录属主和属组
chown -R "$tester"."$oinstall" /oracle
# 修改 oracle 安装目录操作权限
chmod 755 -R /oracle
editenv
else
echo "目录创建失败"
fi
}
# 定义创建用户的函数
createuser(){
# 新建用户,用户录属于 dba 和 oinstall
useradd $tester -g $oinstall -G $dba 1>> /tmp/correct.txt 2>> error.txt
# 查看创建成功后添加到/etc/passwd文件中
finduser=`grep "$tester" /etc/passwd |cut -d : -f 1`
if [ "$finduser" == "$tester" ]
then
echo "用户创建成功"
# 修改密码
echo "$pass" | passwd --stdin $tester 1>> /tmp/correct.txt 2>> /tmp/error.txt
echo "密码修改成功"
#调用函数
createdir
else
echo "用户创建失败"
fi
}
creategrp(){
#创建管理组
groupadd $dba 1>> /tmp/correct.txt 2>> error.txt
finddba=`grep "$dba" /etc/group |cut -d : -f 1`
# 判断管理组是否存在
if [ "$finddba" == "$dba" ]
then
echo "管理组创建成功"
groupadd $oinstall 1>> /tmp/correct.txt 2>> error.txt
# 查找安装组并定义变量
findoinstall=`grep "$oinstall" /etc/group |cut -d : -f 1`
# 判断安装组是否存在
if [ "$findoinstall" == "$oinstall" ]
then
echo "安装组创建成功"
#调用创建用户组函数
createuser
else
echo "安装组创建失败"
fi
else
echo "管理组创建失败"
fi
}
# 判断当前用户是否为ROOT
if [ "$USER" == "root" ]
then
echo "当前用户是root"
creategrp
else
echo "当前用户不是root"
fi