######################################################################################################################
2009-04-29
######################################################################################################################
linux系统中简单命令的学习
1、PS1命令:有利于识别当前所在的数据库服务器机器名以及数据库名以及当前所在的工作路径
语法:
PS1="'hostname'*/${ORACLE_SID}-/${PWD}>"
例子:
[root@localhost ~]# PS1="'2.70'*/${ORACLE_SID}-/${PWD}>"
'2.70'*-/root>su - oracle
[oracle@localhost ~]$ PS1="'2.70'*/${ORACLE_SID}-/${PWD}>"
'2.70'*STAPLES-/home/oracle>cd /opt
'2.70'*STAPLES-/opt>ls
注:将上面的语句可以写在.profile文件中,就可以在做oracle用户切换时自动的可以学友显示
当前所在的数据库服务器机器名以及数据库名以及当前所在的工作路径的信息
2、alias命令:是可以给某个命令起个别名,方便以后执行同样的命令
使用如下:
创建别名
2.70'*STAPLES-/opt/oracle>alias log='cd /opt/oracle/admin/$ORACLE_SID/bdump'
显示所有别名
'2.70'*STAPLES-/opt/oracle>alias
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias log='cd /opt/oracle/admin/$ORACLE_SID/bdump'
alias ls='ls --color=tty'
alias mc='. /usr/share/mc/bin/mc-wrapper.sh'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
直接输入log别名,与执行cd /opt/oracle/admin/$ORACLE_SID/bdump命令同样的效果
'2.70'*STAPLES-/opt/oracle>log
'2.70'*STAPLES-/opt/oracle/admin/STAPLES/bdump>ls -lpth |more
total 93M
-rw-r----- 1 oracle oinstall 2.8M Apr 29 15:41 staples_arc0_3915.trc
-rw-r----- 1 oracle oinstall 616K Apr 29 15:41 staples_arc1_3917.trc
....
列出在oracle用户下经常用到的命令,并且给这些命令都起个别名,方每个oracle用户使用,
只要将这些命令输入到每个oracle用户的.profile文件里面
#Alias
#
alias alert='tail -100 /opt/oracle/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log|more'
alias arch='cd /opt/oracle/admin/$ORACLE_SID/arch'
alias bdump='cd /opt/oracle/admin/$ORACLE_SID/bdump'
alias cdump='cd /opt/oracle/admin/$ORACLE_SID/cdump'
alias pfile='cd /opt/oracle/admin/$ORACLE_SID/pfile'
alias sid='env |grep ORACLE_SID'
alias admin=='cd /opt/oracle/admin'
######################################################################################################################
2009-05-04
######################################################################################################################
3、cut命令的使用
名称:cut
使用权限:所有使用者
用法:cut -c num1-num2 filename
说明:显示每行从开头算起 num1 到 num2 的文字
范例:
shell>> cat example
test2
this is test1
shell>> cut -c1-6 example ## print 开头算起前 6 个字元
test2
this i
[oracle@localhost opt]$ cut -c 1-2,5-6 testcut.sql
te2
th i
-c m-n 表示显示每一行的第m个字元到第n个字元
-c 和 -f 参数可以跟以下子参数:
m 第m个字符或字段
m- 从第m个字符或字段到文件结束
m-n 从第m个到第n个字符或字段
-n 从第1个到第n个字符或字段
我们经常会遇到需要取出分字段的文件的某些特定字段,
例如 /opt/testgrep-cut.sql就是通过":"分隔各个字段的。可以通过cut命令来实现。
[oracle@localhost opt]$ cat /opt/testgrep-cut.sql
STAPLES:/opt/oracle/product/10.2.0:N
例如,我们希望将STAPLES保存到/opt/test.sql特定的文件中,就可以:
[root@localhost opt]# cut -d: -f 1 /opt/testgrep-cut.sql > /opt/test.sql
[root@localhost opt]# cat /opt/test.sql
STAPLES
-d用来定义分隔符,默认为tab键,-f表示需要取得哪个字段
如:
使用:分隔
[root@localhost opt]# cut -d: -f 2 /opt/testgrep-cut.sql
/opt/oracle/product/10.2.0
使用/分隔
[root@localhost opt]# cut -d'/' -f 2 /opt/testgrep-cut.sql
opt
这里使用单引号或双引号皆可。
下面是grep中带有cut命令的使用
[oracle@localhost opt]$ export $ORACLE_SID=STAPLES
[oracle@localhost opt]$ echo $ORACLE_SID
STAPLES
[oracle@localhost opt]$ cat /etc/oratab |more
+ASM:/opt/oracle/product/10.2.0:N
STAPLES:/opt/oracle/product/10.2.0:N
TEMPDB:/opt/oracle/product/10.2.0:N
--查询出含有当前ORACLE_SID关键字的一行数据
[oracle@localhost opt]$ cat /etc/oratab|grep $ORACLE_SID
STAPLES:/opt/oracle/product/10.2.0:N
[oracle@localhost opt]$ cat /etc/oratab|grep $ORACLE_SID:|cut -f2 -d:
/opt/oracle/product/10.2.0
######################################################################################################################
2009-05-05
######################################################################################################################
4、在SHELL脚本中执行SQL脚本
1)脚本如下:
/opt>cat run_sql.ksh
#!/bin/ksh
#set the environment
ORACLE_SID=STAPLES
export ORACLE_SID
ORACLE_HOME=/opt/oracle/product/10.2.0
##/'cat /etc/oratab|grep ^$ORACLE_SID:|cut -f2 -d':''
export ORACLE_HOME
PATH=$ORACLE_HOME/bin:$PATH
export PATH
$ORACLE_HOME/bin/sqlplus system/111111<<!
select t.DBID,t.NAME,t.CREATED from v/$database t;
@/home/oracle/sql/longscript.sql
exit
!
2) 要用chmod 755 run_sql.ksh来将此脚本改成可执行
/opt>ls -l run_sql.ksh
-rw-r--r-- 1 root root 337 5月 5 14:13 run_sql.ksh
/opt>chmod 755 run_sql.ksh
/opt>ls -l run_sql.ksh
-rwxr-xr-x 1 root root 337 5月 5 14:13 run_sql.ksh
3)执行脚本,直接在提示符下调用
/opt>./run_sql.ksh
住:也可以直接使用下面的语句执行@/home/oracle/sql/longscript.sql脚本,如下:
sqlplus system/111111 @/home/oracle/sql/longscript.sql