Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

awk 调用system函数,再调用bash的printf函数

debian8@debian:~$ awk 'BEGIN{system("printf %xn  32")}'  
20ndebian8@debian:~$ 

请看下面
printf %x
  32
20ndebian8@debian:~$ 


awk 'BEGIN{system("printf %x
  32")}'
0sh: 2: 32: not found

请问,这里的 awk 'BEGIN{system("printf %x
  32")}' 为何会有这个结果?

问题没有这么简单哈,再请看

debian8@debian:~$ awk 'BEGIN{system("printf %x\n  32")}'
20ndebian8@debian:~$ 

如何解释?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

请参考linux的system()函数的说明.

在双引号里面的时候代表换行符,即system("printf %x 32")相当于你在命令行里面先后敲了两个命令:

  1. printf %x

  2. 32

对于最后的结果0sh: 2: 32: not found可以拆分为三部分理解

  1. 0printf %x的运行结果

  2. sh: 2:print %x的返回说明

  3. 32: not found 32 的运行报错结果

可以使用system("printf %x 32 ")来试试,就能正确运行无错误


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...