在刪除文件或進行一些亂七八糟的危險操作時總需要用戶手動輸入一個"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)
.