[20200214]xargs與別名.txt

lfree發表於2020-02-14

[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。




$ 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


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部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2675611/,如需轉載,請註明出處,否則將追究法律責任。

相關文章