For DBAs, it's often the case for the upgrade database or apply patch, and sometimes a lot of scripts need to be run. For these operations we want the files that are now being printed on the screen to be exported at the same time as the steps or errors that were lost during the subsequent query. The script command under Linux is a good helper to solve this problem.
1. Script Command Description
Copy Code code as follows:
The
script command records all operations to the file at the same time as the screen output, until the logon session is terminated, or the crtl+d is used, or exit exits with exit to stop the recording.
This command can be used for subsequent query success or failure in the case of database upgrades or important settings.
Usage: $ script [Upgrade.log]
if the log file name is not specified, the automatically generated log file name is typescript.
If you need to output to a log file that already exists, use the-a parameter, and then the log file name already exists.
You can also view the contents of a log file while outputting to a log file, you can use the-f argument. </p> <p># Script Help Description
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 to students who need a hardcopy record of
a interactive session as proof of a 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 was saved in the
file typescript.</p> <p> options:</p> <p>-a Append the output to file or typescript, retain ing the prior contents.</p> <p>-C COMMAND
Run the COMMAND rather than an interactive shell. This is makes it easy for a script to capture the output of a pro-
gram that behaves differently as its stdout are 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 do using ' cat foo ' .</p> <p>-Q be quiet.</p> <p>- T Output timeing data to standard error. This is the data contains two fields, separated by a. The 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 is used to replay typescripts with realistic typing and output delays.
2. Example of script command usage
Copy Code code as follows:
A, script command followed by log file name
robin@szdb:~> script Test_logfile.log #启用script
Script started, the file is Test_logfile.log</p> <p> ..... #中间操作省略
., ...
robin@szdb:~> exit #停止script
Exit
Script Done, the file is test_logfile.log</p> <p> #查看刚刚生成的日志文件
robin@szdb:~> more Test_logfile.log
Script started on Fri 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 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 are 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 SE T current_schema=scott;</p> <p>session altered.</p> <p>goex_admin@SYBO2SZ> SELECT * FROM dept;</p> <p> DEPTNO dname LOC
---------- -------------- -------------
ACCOUNTING NEW YORK
DALLAS
SALES CHICAGO
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 do on Fri Apr 2013 05:46:32 PM CST </p> <p>b, script direct to scripting name
robin@szdb:~> script-c retval.sh
Script started, file is Typescript
7788 SCOTT ANALYST 7566 19870419 00:00:00 3100 20
Script Done, the 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