方法一:system函数
1 | ip add | 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)} |
方法二:使用变量(获取变量的时候已经执行命令)
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
1 2 | [root@rac1 ~] # awk 'BEGIN{print "echo cange"|"bash"}' cang |
1 2 3 4 5 6 7 | [root@rac1 ~] # ls -lh | awk '{print "echo cange"|"bash"}' cange cange cange cange cange cang |
方法三的内容延伸:执行多重命令
1 2 3 4 5 6 7 | [root@rac1 ~] # ls -lh | awk '{print "echo cange `date +%Y-%m-%d`"}' | bash cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-1 |
1 2 3 4 5 6 7 8 9 | [root@rac1 ~] # ls -lh | awk '{print "echo cange $(date +%Y-%m-%d)"}'| bash cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 cange 2021-08-11 |
结合输出引号和shell命令:
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 -n 10 | bash /u01/app/grid/diag/crs/rac1/crs/trace/ 1628211140 1628211123 1628211123 /u01/app/grid/diag/crs/rac1/crs/trace/ocssd .trc 1628153326 1628211421 1628211421 /u01/app/grid/diag/crs/rac1/crs/trace/ocssd .trm 1628153326 1628211421 1628211421 /u01/app/grid/diag/crs/rac1/crs/trace/octssd .trc 1628099149 1628211410 1628211410 /u01/app/grid/diag/crs/rac1/crs/trace/octssd .trm 1628099149 1628211410 1628211410 /u01/app/grid/diag/crs/rac1/crs/trace/ohasd_orarootagent_root .trc 1628206771 1628211421 1628211421 /u01/app/grid/diag/crs/rac1/crs/trace/crsd_oraagent_grid .trc 1628044975 1628211421 1628211421 /u01/app/grid/diag/crs/rac1/crs/trace/ohasd .trc 1628202286 1628211419 1628211419 /u01/app/grid/diag/crs/rac1/crs/trace/crsd .trc 1628115223 1628211414 1628211414 /u01/app/grid/diag/crs/rac1/crs/trace/crsd_oraagent_oracle .trc 1627639317 1628211417 162821141 |
##%分号可以放在双引号内正常输出,
标签:11,crs,shell,技巧,rac1,08,cange,awk,2021
来源: https://blog.csdn.net/wubaohu1314/article/details/120099033