ITPub博客

首页 > 数据库 > Oracle > [20200214]xargs与别名.txt

[20200214]xargs与别名.txt

原创 Oracle 作者:lfree 时间:2020-02-14 16:17:11 0 删除 编辑

[20200214]xargs与别名.txt

--//上午在优化sql语句时,发现xargs与alias的程序存在一点点小问题,做一个记录。

$ alias rrlsql='rlwrap sqlplus '
$ rrlsql -s -l scott/book @ ver1 <<< ' '

PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

$ echo @ver1 | xargs -I{} rrlsql scott/book {}
xargs: rrlsql: No such file or directory
--//也就是xargs不支持别名,应该是另外开一个shell,里面的环境与登录的bash shell环境不一致。

$ echo @ver1 | xargs -I{} sqlplus -s -l scott/book {}
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

$ alias ls
alias ls='ls --color=auto --time-style=+"%Y-%m-%d %H:%M:%S"'

$ echo bb1.txt | xargs ls -l
-rw-r--r-- 1 oracle oinstall 230 Feb 12 16:03 bb1.txt

$ ls -l bb1.txt
-rw-r--r-- 1 oracle oinstall 230 2020-02-12 16:03:45 bb1.txt
--//注意我显示的日期格式,执行时并没有现在别名ls。

https://stackoverflow.com/questions/34795432/using-xargs-to-pass-a-variable-to-alias-command
https://stackoverflow.com/questions/979453/how-can-i-use-aliased-commands-with-xargs

$ echo bb1.txt | xargs -IQ bash -c 'ls -l Q'
-rw-r--r-- 1 oracle oinstall 230 Feb 12 16:03 bb1.txt
--//不行。

$ echo bb1.txt | xargs -IQ bash -ic 'ls -l Q'
-rw-r--r-- 1 oracle oinstall 230 2020-02-12 16:03:45 bb1.txt

https://stackoverflow.com/questions/979453/how-can-i-use-aliased-commands-with-xargs
Aliases are shell-specific - in this case, most likely bash-specific. To execute an alias, you need to execute bash, but
aliases are only loaded for interactive shells (more precisely, .bashrc will only be read for an interactive shell).

bash -i runs an interactive shell (and sources .bashrc). bash -c cmd runs cmd.

--//我必须讲别名放入.bashrc文件中,重新登录打开新的终端:
--//加入如下.bashrc.
alias rrlsql='rlwrap sqlplus '

$ echo @ver1 | xargs -IQ bash -ci 'rrlsql -s -l scott/book Q'

PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
--//OK!!

--//在测试中我还遇到一个问题。

$ echo 4xamnunv51w9j | xargs -IQ bash -ci rrlsql -s -l scott/book @dpc Q ''
SQL*Plus: Release 11.2.0.4.0 Production on Fri Feb 14 16:07:46 2020
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
Enter user-name:

--//必须加双引号才OK。
$ echo 4xamnunv51w9j | xargs -IQ bash -ci "rrlsql -s -l scott/book @dpc Q ''"
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4xamnunv51w9j, child number 0
-------------------------------------
select * from dept where deptno=10
Plan hash value: 2852011669
----------------------------------------------------------------------------------------
| Id  | Operation                   | Name    | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |         |        |       |     1 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |      1 |    20 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | PK_DEPT |      1 |       |     0   (0)|          |
----------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / DEPT@SEL$1
   2 - SEL$1 / DEPT@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("DEPTNO"=10)
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level
31 rows selected.
argment : typical all advanced partition predicate remote note parallel projection alias peeked_binds outline adaptive

--//另外的问题:
--//我的rlsql定义的是函数
$ echo 4xamnunv51w9j | xargs -IQ  rlsql  -s -l scott/book @dpc Q ''
Enter value for 2:
SP2-0546: User requested Interrupt or EOF detected.

argment : typical all advanced partition predicate remote note parallel projection alias peeked_binds outline adaptive

--//最后1个参数''无法解析,加入引号或者\转义可以通过。
$ echo 4xamnunv51w9j | xargs -IQ  rlsql  -s -l scott/book @dpc Q "''"
$ echo 4xamnunv51w9j | xargs -IQ  rlsql  -s -l scott/book @dpc Q \'\'
$ echo 4xamnunv51w9j | xargs -IQ  rlsql  -s -l scott/book @dpc Q all

来自 “ ITPUB博客 ” ,链接:/267265/viewspace-2675611/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    2673
  • 访问量
    6432673