Linux下通过script 命令记录(数据库)操作步骤
程序员文章站
2022-10-13 19:33:30
对DBA而言,经常碰到升级数据库或是apply patch,以及有些时候需要运行大量的脚本。对于这些操作我们希望现在在屏幕同时又输出的文件以备后续查询过程中曾经丢失的步骤或错误。Linux... 13-08-04...
1、script命令描述
复制代码
代码如下:script命令会记录所有的操作到文件同时在屏幕上输出,直到终止登陆的会话,或使用crtl+d,或使用exit退出则停止记录。
这个命令对于数据库的升级或是重要设置的情形下使用可以用于后续查询操作成功或失败。
用法: $ script [upgrade.log]
如果未指定日志文件名的情形,自动生成日志文件名为typescript。
如果需要输出到已经存在的日志文件,则使用 -a 参数,再接已经存在日志文件名。
如果需要在输出到日志文件的同时,也可以查看日志文件的内容,可以使用 -f 参数。</p> <p># script 帮助描述
robin@szdb:~> man script
name
script - make typescript of terminal session</p> <p>synopsis
script [-a] [-c command] [-f] [-q] [-t] [file]</p> <p>description
script makes a typescript of everything printed on your terminal. it is useful for students who need a hardcopy record of
an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).</p> <p> if the argument file is given, script saves all dialogue in file. if no file name is given, the typescript is saved in the
file typescript.</p> <p> options:</p> <p> -a append the output to file or typescript, retaining the prior contents.</p> <p> -c command
run the command rather than an interactive shell. this makes it easy for a script to capture the output of a pro-
gram that behaves differently when its stdout is not a tty.</p> <p> -f flush output after each write. this is nice for telecooperation: one person does `mkfifo foo; script -f foo' and
another can supervise real-time what is being done using `cat foo'.</p> <p> -q be quiet.</p> <p> -t output timeing data to standard error. this data contains two fields, separated by a space. the first field indi-
cates how much time elapsed since the previous output. the second field indicates how many characters were output
this time. this information can be used to replay typescripts with realistic typing and output delays.
2、script命令用法示例
复制代码
代码如下:a、script命令后接日志文件名
robin@szdb:~> script test_logfile.log #启用script
script started, file is test_logfile.log</p> <p> .................#中间操作省略
robin@szdb:~> exit #停止script
exit
script done, file is test_logfile.log</p> <p>#查看刚刚生成的日志文件
robin@szdb:~> more test_logfile.log
script started on fri 26 apr 2013 05:45:23 pm cst
robin@szdb:~> sid
bash: sid: command not found
robin@szdb:~> . ~/.bash_profile
robin@szdb:~> sid
ora_crs_home=/opt/oracle/product/10gr2/crs
oracle_path=.:/users/robin/dba_scripts/custom/sql
ora_asm_home=/opt/oracle/product/10gr2/asm
oracle_sid=sybo2sz
oracle_base=/users/oracle
oracle_home=/users/oracle/orahome10g
robin@szdb:~> sql </p> <p>sql*plus: release 10.2.0.3.0 - production on fri apr 26 17:46:12 2013</p> <p>copyright (c) 1982, 2006, oracle. all rights reserved.</p> <p>error accessing product_user_profile
warning: product user profile information not loaded!
you may need to run pupbld.sql as system</p> <p>connected to:
oracle database 10g release 10.2.0.3.0 - 64bit production</p> <p>goex_admin@sybo2sz> alter session set current_schema=scott;</p> <p>session altered.</p> <p>goex_admin@sybo2sz> select * from dept;</p> <p> deptno dname loc
---------- -------------- -------------
10 accounting new york
20 research dallas
30 sales chicago
40 operations boston</p> <p>goex_admin@sybo2sz> exit
disconnected from oracle database 10g release 10.2.0.3.0 - 64bit production
robin@szdb:~> exit
exit</p> <p>script done on fri 26 apr 2013 05:46:32 pm cst </p> <p>b、script 命下直接跟脚本名的示例
robin@szdb:~> script -c retval.sh
script started, file is typescript
7788 scott analyst 7566 19870419 00:00:00 3100 20
script done, file is typescript</p> <p>#被执行的shell脚本的内容
# author : robinson
# blog : http://blog.csdn.ner/robinson_0612</p> <p>robin@szdb:~> more ~/dba_scripts/custom/bin/retval.sh
#!/bin/bash
retval=`sqlplus -silent scott/tiger <<eof
set pagesize 0 feedback off verify off heading off echo off
select * from emp where ename='scott';
exit;
eof`
if [ -z "$retval" ]; then
echo "no rows returned from database"
exit 0
else
echo $retval
fi