重复输出字符串
来源:互联网 发布:python web service 编辑:程序博客网 时间:2024/05/22 19:26
本文链接:http://codingstandards.iteye.com/blog/826940 (转载请注明出处)
用途说明
yes命令用于重复输出字符串(output a string repeatedly until killed)。这个命令可以帮你自动回答命令行提示,例如,进入一个含有多个文件的目录,执行 "yes | rm -i *",所有的 rm: remove regular empty file `xxx'? 提示都会被自动回答 y。这在编写脚本程序的时候会很用处。yes命令还有另外一个用途,可以用来生成大的文本文件。
常用参数
yes命令不指定参数时,不断的输出y;指定字符串参数时,就不断的输出该字符串。要终止输出,必须杀掉该进程,比如按Ctrl+C,或killall yes。(Repeatedly output a line with all specified STRING(s), or ‘y’.)比如:要不断输出n时,输入yes n。
使用示例
示例一 删除文件时自动回答y
[root@web ~]# ls -l *.txt
-rw-r--r-- 1 root root 7 11-28 11:54 1.txt
-rw-r--r-- 1 root root 10217 07-06 13:10 data.txt
[root@web ~]# yes | rm -i *.txt
rm:是否删除 一般文件 "1.txt" | rm -i.txt”? rm:是否删除 一般文件 “data.txt”? [root@web ~]# yes | rm -i *.txt
rm: lstat “*.txt” 失败: 没有那个文件或目录
[root@web ~]# ls -l *.txt
ls: *.txt: 没有那个文件或目录
[root@web ~]#
示例二 生成大的文本文件
下面的脚本把yes命令输出的内容保存到文件中,然后1秒钟之后停止输出。在这台测试机器上,生成了一个93M的文件。
- #!/bin/sh
- yes hello >hello.txt &
- PID=$!
- sleep 1
- kill $PID
- ls -l hello.txt
[root@web ~]# cat yes.sh
#!/bin/sh
yes hello >hello.txt &
PID=$!
sleep 1
kill $PID
ls -l hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 93003776 11-28 14:02 hello.txt
./yes.sh: line 9: 5771 已终止 yes hello > hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 95346688 11-28 14:09 hello.txt
./yes.sh: line 9: 7072 已终止 yes hello > hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 0 11-28 14:09 hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 0 11-28 14:09 hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 0 11-28 14:09 hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 94040064 11-28 14:10 hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 0 11-28 14:10 hello.txt
[root@web ~]# ./yes.sh
-rw-r--r-- 1 root root 0 11-28 14:10 hello.txt
[root@web ~]#
问题出现了:如果频繁的执行这个脚本,就会发现竟然生成0字节的文件,为何?
问题思考
1. 请分析解释上面的yes.sh脚本频繁执行时的奇怪现象。
- 重复输出字符串
- 一些练习——输出重复字符串
- 输出字符串中的所有重复子
- 输出一个字符串的所有自字符串(不重复)
- Java基础之输出字符串中重复、不重复、消除重复后字符
- 输出一个字符串所有排列。注意有重复字符
- 给定一个字符串,输出最长的重复子串
- 找出字符串的最长不重复子串,输出长度
- 给定一个字符串,输出最长的重复子串
- 按照字典的顺序输出字符串,并不允许重复
- 找出字符串最长不重复子串,输出长度
- Linux yes --重复输出字符串,自动回答命令行提示
- 统计字符串中重复的字符个数并输出
- 找出字符串的最长不重复子串,输出长度
- 输出重复出现字符串中最多的一个 C++实现
- 【JavaScript】面试题:重复输出一个给定的字符串
- 找出字符串的最长不重复子串,输出长度
- JavaScript 面试题:重复输出一个给定的字符串
- android上gdb coredump步骤
- 面试中常见的C/C++问题集锦
- DB2错误信息(按sqlcode排序)
- 了解事件的类型
- POJ1743--Musical Theme
- 重复输出字符串
- Manifest文件
- 脚本语言
- AIX如何开机自动重新启动程序
- JS左移右移
- VC++实现ARP协议
- android之SharedPreferences
- About iOS webview load commpletly
- spring MVC访问路径误区