早教吧 育儿知识 作业答案 考试题库 百科 知识分享

一个批处理文件想转换成shell,@echooff&setlocalenabledelaydexpansionset/Poldusername='Pleaseenteryourusernamefortheolddatabase:'echoYourinputis:%oldusername%set/Pnewusername='Pleaseenteryourusernameforthenewdatabase:'e

题目详情
一个批处理文件想转换成shell,
@echo off&setlocal enabledelaydexpansion
set /P oldusername='Please enter your username for the old database:'
echo Your input is:%oldusername%
set /P newusername='Please enter your username for the new database:'
echo Your input is:%newusername%
set /P newuserpassword='Please enter your password for the new database:'
echo Your input is:%newuserpassword%
set /P orcl='Please enter the oracle service name:'
echo Your input is:%orcl%
set /P syspassword='Please enter the password for sys:'
echo.
echo.
echo Moving the data,Please wait...
echo.
set str=rbtdb_16
for /f "delims=" %%i in ('dir/b script.sql')do (
(for /f "usebackq delims=" %%j in ("%%i")do (
set "a=%%j"
\x05set "a=!a:%str%=%oldusername%!"
\x05echo.))>02_script.sql)
echo conn sys/%syspassword%@%orcl% as sysdba>01_conn.sql
echo grant select any table to %newusername%;>>01_conn.sql
echo conn %newusername%/%newuserpassword%@%orcl%>>01_conn.sql
echo exit>>01_conn.sql
sqlplus /nolog @01_conn.sql
del /q 01_conn.sql
del /q 02_script.sql
pause
@echo on&endlocal
此批处理的作用是输入新的用户名和旧的用户名,替换script.sql里的用户名,然后执行脚本,进行数据迁移,script.sql里的用户名是写死的,就是rbtdb_16.批处理将输入的旧用户名替换掉此用户名然后执行脚本.
现在想转换成shell脚本,但本人shell还没接触过,时间又比较紧,
对脚本的要求是,尽量和批处理的效果一样,尽量不改变原文件即script.sql,替换完用户名后可以产生临时文件02_script.sql,执行完后最好自动删除临时文件01_conn.sql,02_script.sql.
坐等啊.
继续等.
▼优质解答
答案和解析
printf 'Please enter your username for the old database:'
read oldusername
echo Your input is:$oldusername
printf 'Please enter your username for the new database:'
read newusername
echo Your input is:$newusername
printf 'Please enter your password for the new database:'
read newuserpassword
echo Your input is:$newuserpassword
printf 'Please enter the oracle service name:'
read orcl
echo Your input is:$orcl
printf 'Please enter the password for sys:'
read syspassword
echo
echo
echo Moving the data,Please wait...
echo
str=rbtdb_16
sed 's/'"$str"'/'"$oldusername"'/g' script.sql >02_script.sql
echo conn sys/$syspassword@$orcl as sysdba>01_conn.sql
echo grant select any table to $newusername;>>01_conn.sql
echo conn $newusername/$newuserpassword@$orcl>>01_conn.sql
echo exit>>01_conn.sql
sqlplus --nolog @01_conn.sql
rm 01_conn.sql
rm 02_script.sql
printf "press any key to exit"
read -n 1

02_script.sql生成 后没有用又删除了是什么 意思 ?,除了sqlplus命令不知咋转换,其余的都转 换成了shell