Fork me on GitHub

Linux命令之yes

 

在删除文件或进行一些乱七八糟的危险操作时总需要用户手动输入一个"y"来确定一下,但是如果数量很多的话手动输入就是个灾难啊,yes最初就是为了解决这个问题产生的,比如这种形式:

yes | rm a*

当然我们可以指定-f静默选项不进行确认询问,呃,就当我举了个不太恰当的例子吧,毕竟我删除文件从来都是-f选项,而借助yes的话太麻烦了。

 

不过它的另一个用法可能大多数人都不知道,就是可以指定一行字符串然后不断将这行字符串送到标准输出,实际上yes本来的作用就是接收一行字符串然后不断将它输出,如果未指定的话默认就是y,这个是写在手册里的:

Repeatedly output a line with all specified STRING(s), or 'y'.

所以没事啃下手册还是有些用处的,这个特性可以将yes当做“crazy-repeat”使用(我瞎取的名字,不过还挺形象的),即将一行字符串不断的重复输出到标准输出:

yes "foobar"

在某些情况下可以用来模拟数据产生的过程,比如模拟日志疯狂产生的过程:

yes "[2018-08-08 18:30:17] Thread-10 : foo bar"

 

总结:

1. yes用来将指定字符串不断送到标准输出,如果未指定字符串的话默认就是y,如果进程不被杀掉会一直发送直到天荒地老。

2. 在某些情况下可以用来模拟不断产生的数据,优点是实现简单,缺点是每行数据都是一样的不会变,不如写个脚本模拟。

 

 

最后附上文档man yes:

YES(1)                                                            User Commands                                                           YES(1)

NAME
       yes - output a string repeatedly until killed

SYNOPSIS
       yes [STRING]...
       yes OPTION

DESCRIPTION
       Repeatedly output a line with all specified STRING(s), or 'y'.

       --help display this help and exit

       --version
              output version information and exit

       GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report yes translation bugs to <http://translationproject.org/team/>

AUTHOR
       Written by David MacKenzie.

COPYRIGHT
       Copyright © 2013 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       The  full documentation for yes is maintained as a Texinfo manual.  If the info and yes programs are properly installed at your site, the
       command

              info coreutils 'yes invocation'

       should give you access to the complete manual.

GNU coreutils 8.22                                                 April 2018                                                             YES(1)

 

.

posted @ 2018-08-26 23:58  CC11001100  阅读(2756)  评论(0编辑  收藏  举报