相关疑难解决方法(0)

“是”命令的意义是什么?

这个问题与yesUNIX 和 Linux 机器中的命令有关:基本上,这个工具的意义(如果有的话)和历史是什么?它有实际应用吗?是否可以显示一个示例在脚本中有用的地方或与其他工具链接(通过管道或重定向)的地方?

手册页如下:

YES(1)                    BSD General Commands Manual                   YES(1)

NAME
     yes -- be repetitively affirmative

SYNOPSIS
     yes [expletive]

DESCRIPTION
     yes outputs expletive, or, by default, ``y'', forever.

HISTORY
     The yes command appeared in 4.0BSD.

4th Berkeley Distribution        June 6, 1993        4th Berkeley Distribution
Run Code Online (Sandbox Code Playgroud)

示例输出:

$ yes why
why
why
why
why
^Cwhy
Run Code Online (Sandbox Code Playgroud)

utilities

92
推荐指数
3
解决办法
6万
查看次数

“是”如何如此快速地写入文件?

让我举个例子吧:

$ timeout 1 yes "GNU" > file1
$ wc -l file1
11504640 file1
Run Code Online (Sandbox Code Playgroud)

$ for ((sec0=`date +%S`;sec<=$(($sec0+5));sec=`date +%S`)); do echo "GNU" >> file2; done
$ wc -l file2
1953 file2
Run Code Online (Sandbox Code Playgroud)

在这里您可以看到该命令在一秒钟内yes写入11504640行,而我只能1953在 5 秒内使用 bashforecho.

正如评论中所建议的,有各种技巧可以提高效率,但没有一个能与以下速度相媲美yes

$ ( while :; do echo "GNU" >> file3; done) & pid=$! ; sleep 1 ; kill $pid
[1] 3054
$ wc -l file3
19596 file3
Run Code Online (Sandbox Code Playgroud)

$ timeout 1 bash -c 'while …
Run Code Online (Sandbox Code Playgroud)

bash coreutils write yes

61
推荐指数
3
解决办法
8823
查看次数

标签 统计

bash ×1

coreutils ×1

utilities ×1

write ×1

yes ×1