gpt4 book ai didi

linux - $0 在下面的 bash 脚本中代表什么?

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:23 24 4
gpt4 key购买 nike

第一个脚本

seq 9 | awk '{ lifo[NR]=$0 } END{for{lno=NR; lno>-1 lno--}print lifo[lno];}'

第二个脚本

egrep -o "\b[[:alpha:]]+\b" $filename |\ 

awk '{count[$0]++'}

END {
for(ind in count)


{printf ("%-14s%d\n",ind,count[int]);}

}'

第一个示例是反向打印文件,第二个示例是计算文件中的唯一单词并将它们与编号一起打印。那里的外观。

文件

sam 
dad
dad

第二个输出:

sam 1

dad 2

在这两个脚本中,$0 是如何工作的?echo $0 打印使用的 shell。

最佳答案

seq 9 | awk '{ lifo[NR]=$0 } END{for{lno=NR; lno>-1 lno--}print lifo[lno];}'

请看单引号,它们避免 shell 解释 $0,然后 awk 将其解释为 整个输入行

关于linux - $0 在下面的 bash 脚本中代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com