awk小技巧之執行shell命令
阿新 • • 發佈:2021-09-04
方法一:system函式
方法二:使用變數(獲取變數的時候已經執行命令)
ls |awk '{print i$0}' i=`pwd`'/'
或
[root@rac1 ~]# ls -a |awk '{print i$0}' i="`pwd`/"注意:這裡的雙引號不可以用單引號替換,單引號內強制為字串-不執行引號內的命令。
[root@rac1 ~]# ls -a |awk -v i="`pwd`/" '{print i$0}'
方法三:把命令傳遞給bash
方法三的內容延伸:執行多重命令
結合輸出引號和shell命令:
##%分號可以放在雙引號內正常輸出,
1 |
ipadd| grep enp0s8| grep inet| awk '{print$2}' | awk -F "/" '{ printf $1 "" }{cmd= "hostname" ;system(cmd)}
|
1 |
[root@rac1~] #ls|awk'{printf$NF""}{cmd="hostname";system(cmd)}
|
1 2 |
[root@rac1~] #awk'BEGIN{print"echocange"|"bash"}'
cang
|
1 2 3 4 5 6 7 |
[root@rac1~] #ls-lh|awk'{print"echocange"|"bash"}'
cange
cange
cange
cange
cange
cang
|
1 2 3 4 5 6 7 |
[root@rac1~] #ls-lh|awk'{print"echocange`date+%Y-%m-%d`"}'|bash
cange2021-08-11
cange2021-08-11 cange2021-08-11
cange2021-08-11
cange2021-08-11
cange2021-08-1
|
1 2 3 4 5 6 7 8 9 |
[root@rac1~] #ls-lh|awk'{print"echocange$(date+%Y-%m-%d)"}'|bash
cange2021-08-11
cange2021-08-11
cange2021-08-11
cange2021-08-11
cange2021-08-11
cange2021-08-11
cange2021-08-11
cange2021-08-11
|
1 2 3 4 5 6 7 8 9 10 11 |
[grid@rac1~]$ find $ORACLE_BASE /diag/crs/ ` hostname ` /crs/trace/ -ctime-1| awk '{print"echo"$0"`stat-c""\"""%X%Y%Z""\""""$0"`"}' | head -n10| bash
/u01/app/grid/diag/crs/rac1/crs/trace/ 162821114016282111231628211123
/u01/app/grid/diag/crs/rac1/crs/trace/ocssd .trc162815332616282114211628211421
/u01/app/grid/diag/crs/rac1/crs/trace/ocssd .trm162815332616282114211628211421
/u01/app/grid/diag/crs/rac1/crs/trace/octssd .trc162809914916282114101628211410
/u01/app/grid/diag/crs/rac1/crs/trace/octssd .trm162809914916282114101628211410
/u01/app/grid/diag/crs/rac1/crs/trace/ohasd_orarootagent_root .trc162820677116282114211628211421
/u01/app/grid/diag/crs/rac1/crs/trace/crsd_oraagent_grid .trc162804497516282114211628211421
/u01/app/grid/diag/crs/rac1/crs/trace/ohasd .trc162820228616282114191628211419
/u01/app/grid/diag/crs/rac1/crs/trace/crsd .trc162811522316282114141628211414
/u01/app/grid/diag/crs/rac1/crs/trace/crsd_oraagent_oracle .trc16276393171628211417162821141
|