[20210510]變態的windows批處理7.txt

lfree發表於2021-05-12

[20210510]變態的windows批處理7.txt

--//想寫一個批處理在windows下,遇到一個奇怪的問題,記錄自己的分析過程.

d:\>seq  3 | xargs -iQ echo "alter session set optimizer_index_cost_adj = Q;\nselect count(name) from from t where id=1;"
;lter session set optimizer_index_cost_adj = 1
select count(name) from from t where id=1;
;lter session set optimizer_index_cost_adj = 2
select count(name) from from t where id=1;
;lter session set optimizer_index_cost_adj = 3
select count(name) from from t where id=1;

--//很奇怪,分號跑到最前面,什麼回事,加入多個空格,繼續測試:

d:\>seq  3 | xargs -iQ echo "alter session set optimizer_index_cost_adj = Q  ;\nselect count(name) from from t where id=1;"
 ;ter session set optimizer_index_cost_adj = 1
select count(name) from from t where id=1;
 ;ter session set optimizer_index_cost_adj = 2
select count(name) from from t where id=1;
 ;ter session set optimizer_index_cost_adj = 3
select count(name) from from t where id=1;

--//視乎Q後面的字元都會到前面.

d:\>seq  3 | xargs -iQ echo "alter session set optimizer_index_cost_adj = Q  ;\nselect count(name) from from t where id=1;" > aa.txt

--//使用vim下開啟發現問題所在:
alter session set optimizer_index_cost_adj = 1
 ;
select count(name) from from t where id=1;
alter session set optimizer_index_cost_adj = 2
 ;
select count(name) from from t where id=1;
alter session set optimizer_index_cost_adj = 3
 ;
select count(name) from from t where id=1;

--//可以發現後面多了一個^M,實際上就是回車字元\r.導致顯示輸出感覺分號跑到前面.
--//實際上問題出在seq的輸出以及windows對於文字格式的處理,windows文字預設是回車換行.
--//對於windows下的seq的輸出也包含回車換行與linux的seq輸出有一點點不通.知道問題所在解決就很簡單.加入一個tr處理就ok了.

d:\>seq  3 | tr -d '\r' |xargs -iQ echo "alter session set optimizer_index_cost_adj = Q;\nselect count(name) from from t where id=1;"
alter session set optimizer_index_cost_adj = 1;
select count(name) from from t where id=1;
alter session set optimizer_index_cost_adj = 2;
select count(name) from from t where id=1;
alter session set optimizer_index_cost_adj = 3;
select count(name) from from t where id=1;

--//另外一個細節要注意xargs後面呼叫的命令echo是外部命令,windows下的內部命令echo也很變態.看下面的例子:

d:\>echo '123' "456"
'123' "456"

d:\>D:\tools\linux\usr\local\wbin\echo  '123' "456"
'123' 456

--//注意看兩者的區別,windows下命令列僅僅將雙引號的作為字串,而單引號不是.導致awk命令在windows寫的很難看或者難寫.
--//我個人建議widnows下還是在Cygwin64 Terminal下使用來寫shell指令碼.看下面的輸出:

$ echo '123' "456"
123 456

--//總之,windows下會變得非常麻煩.







來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2771852/,如需轉載,請註明出處,否則將追究法律責任。

相關文章