shell脚本登陆数据库

#!/bin/bash

Help()
{
    cat << EOF
 
    Please user this shell like this:
    ./a1.sh dbname dbuser
    
EOF

   exit 1
}
while getopts c:d:e:f: OPT
do
    case $OPT in
        c)
        echo "you put params c and value :$OPTARG"
        ;;
        d)
        echo "you put params d and value :$OPTARG"
        ;;
        e)
        echo "you put params e and value :$OPTARG"
        ;;
        f)
        echo "you put params f and value :$OPTARG"
        ;;
        *)
        Help
        ;;
    esac
done

if [ $# -ne 2 ]
then    
    Help
fi

dbname=$1
dbuser=$2

echo "please input $dnuser password:"
read -s passwd

echo "your dbname is :$dbname and dbuser is :$dbuser and passwd is :$passwd" |tee djk.log
echo "begin to connection db and time now is `date '+%Y%m%d%H%M%S'`" |tee -a djk.log
export ORACLE_SID=$dbname
export ORACLE_HOME=/opt/oracle/product/11gR2/db
/opt/oracle/product/11gR2/db/bin/sqlplus -L  $dbuser/$passwd < ef

if [ $? -ne 0 ]
then
    echo "connection db fail please check your name and password and we will exist " |tee -a djk.log
fi

/opt/oracle/product/11gR2/db/bin/sqlplus $dbuser/$passwd <  set heading off
 set term off
 spool test.log
 select * from omcdevbasicinfo;
 spool off
ef




这里要注意的是 执行这个shell脚本的主机要安装oracle ,export ORACLE_SID=$dbname
export ORACLE_HOME=/opt/oracle/product/11gR2/db这2句话是导入了oracle的坏境变量,必须导入 否则执行sqlplus的时候将会报命令没找到错误




#!/bin/sh
if [ ! -e check.txt ]; then
        echo "check.txt is not exist...."
        exit
fi

dbip=$( grep -i dbip check.txt | awk -F"=" '{print $2}' )
user=$( grep -i user check.txt | awk -F"=" '{print $2}' )
password=$( grep -i password check.txt |awk -F"=" '{print $2}' )

echo "read params dbip:$dbip;user:$user;password:$password"

scp djk.txt $dbip:/opt/oracle/

ssh $dbip " cd /opt/oracle/;chmod 777 djk.txt;su - oracle -c \"

sqlplus -s $user/\"$password\"  <


你可能感兴趣的:(linux)