ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Linux script 命令记录(数据库)操作步骤

Linux script 命令记录(数据库)操作步骤

Linux操作系统 作者:us_yunleiwang 时间:2013-12-05 17:01:32 0 删除 编辑

   对DBA而言,经常碰到升级数据库或是apply patch,以及有些时候需要运行大量的脚本。对于这些操作我们希望现在在屏幕同时又输出的文件以备后续查询过程中曾经丢失的步骤或错误。Linux下的script命令就是解决这个问题的好帮手。

 

1、script命令描述

  1. script命令会记录所有的操作到文件同时在屏幕上输出,直到终止登陆的会话,或使用CRTL+D,或使用exit退出则停止记录。  
  2. 这个命令对于数据库的升级或是重要设置的情形下使用可以用于后续查询操作成功或失败。  
  3. 用法: $ script [upgrade.log]  
  4. 如果未指定日志文件名的情形,自动生成日志文件名为typescript。  
  5. 如果需要输出到已经存在的日志文件,则使用 -a 参数,再接已经存在日志文件名。  
  6. 如果需要在输出到日志文件的同时,也可以查看日志文件的内容,可以使用 -f 参数。  
  7.   
  8. # script 帮助描述  
  9. robin@SZDB:~> man script  
  10. NAME  
  11.      script - make typescript of terminal session  
  12.   
  13. SYNOPSIS  
  14.      script [-a] [-c COMMAND] [-f] [-q] [-t] [file]  
  15.   
  16. DESCRIPTION  
  17.      Script makes a typescript of everything printed on your terminal.  It is useful for students who need a hardcopy record of  
  18.      an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).  
  19.   
  20.      If the argument file is given, script saves all dialogue in file.  If no file name is given, the typescript is saved in the  
  21.      file typescript.  
  22.   
  23.      Options:  
  24.   
  25.      -a      Append the output to file or typescript, retaining the prior contents.  
  26.   
  27.      -c COMMAND  
  28.              Run the COMMAND rather than an interactive shell.  This makes it easy for a script to capture the output of a pro-  
  29.              gram that behaves differently when its stdout is not a tty.  
  30.   
  31.      -f      Flush output after each write. This is nice for telecooperation: One person does `mkfifo foo; script -f foo' and  
  32.              another can supervise real-time what is being done using `cat foo'.  
  33.   
  34.      -q      Be quiet.  
  35.   
  36.      -t      Output timeing data to standard error. This data contains two fields, separated by a space. The first field indi-  
  37.              cates how much time elapsed since the previous output. The second field indicates how many characters were output  
  38.              this time. This information can be used to replay typescripts with realistic typing and output delays.  

2、script命令用法示例

  1. a、script命令后接日志文件名  
  2. robin@SZDB:~> script test_logfile.log        #启用script  
  3. Script started, file is test_logfile.log  
  4.   
  5.   .................#中间操作省略  
  6.     
  7. robin@SZDB:~> exit    #停止script  
  8. exit  
  9. Script done, file is test_logfile.log  
  10.   
  11. #查看刚刚生成的日志文件  
  12. robin@SZDB:~> more test_logfile.log   
  13. Script started on Fri 26 Apr 2013 05:45:23 PM CST  
  14. robin@SZDB:~> sid    
  15. bash: sid: command not found  
  16. robin@SZDB:~> . ~/.bash_profile  
  17. robin@SZDB:~> sid  
  18. ORA_CRS_HOME=/opt/oracle/product/10gR2/crs  
  19. ORACLE_PATH=.:/users/robin/dba_scripts/custom/sql  
  20. ORA_ASM_HOME=/opt/oracle/product/10gR2/asm  
  21. ORACLE_SID=SYBO2SZ  
  22. ORACLE_BASE=/users/oracle  
  23. ORACLE_HOME=/users/oracle/OraHome10g  
  24. robin@SZDB:~> sql       
  25.   
  26. SQL*Plus: Release 10.2.0.3.0 - Production on Fri Apr 26 17:46:12 2013  
  27.   
  28. Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.  
  29.   
  30. Error accessing PRODUCT_USER_PROFILE  
  31. Warning:  Product user profile information not loaded!  
  32. You may need to run PUPBLD.SQL as SYSTEM  
  33.   
  34. Connected to:  
  35. Oracle Database 10g Release 10.2.0.3.0 - 64bit Production  
  36.   
  37. goex_admin@SYBO2SZ> alter session set current_schema=scott;  
  38.   
  39. Session altered.  
  40.   
  41. goex_admin@SYBO2SZ> select * from dept;  
  42.   
  43.     DEPTNO DNAME          LOC  
  44. ---------- -------------- -------------  
  45.         10 ACCOUNTING     NEW YORK  
  46.         20 RESEARCH       DALLAS  
  47.         30 SALES          CHICAGO  
  48.         40 OPERATIONS     BOSTON  
  49.   
  50. goex_admin@SYBO2SZ> exit  
  51. Disconnected from Oracle Database 10g Release 10.2.0.3.0 - 64bit Production  
  52. robin@SZDB:~> exit  
  53. exit  
  54.   
  55. Script done on Fri 26 Apr 2013 05:46:32 PM CST    
  56.   
  57. b、script 命下直接跟脚本名的示例  
  58. robin@SZDB:~> script -c retval.sh  
  59. Script started, file is typescript  
  60. 7788 SCOTT ANALYST 7566 19870419 00:00:00 3100 20  
  61. Script done, file is typescript  
  62.   
  63. #被执行的shell脚本的内容  
  64. # Author : Robinson  
  65. # Blog   : http://blog.csdn.ner/robinson_0612  
  66.   
  67. robin@SZDB:~> more ~/dba_scripts/custom/bin/retval.sh  
  68. #!/bin/bash  
  69. RETVAL=`sqlplus -silent scott/tiger <
  70. SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF  
  71. SELECT * FROM emp WHERE ename='SCOTT';  
  72. EXIT;  
  73. EOF`  
  74. if [ -z "$RETVAL" ]; then  
  75.   echo "No rows returned from database"  
  76.   exit 0  
  77. else  
  78.   echo $RETVAL  
  79. fi  

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23490154/viewspace-1062424/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-03-15

  • 博文量
    198
  • 访问量
    1393150