最近跟Linux下的一些脚本测试打交道比较多,踩了不少坑,也总结了不少经验,在此记录一下,做个备忘。
1.如何创建大量测试数据?(单个大文件)
搜索关键字:http://search.aol.com/aol/search?q=linux+how+to+make+big+file+for+test
参考链接:
- http://stackoverflow.com/questions/257844/quickly-create-a-large-file-on-a-linux-system
- http://superuser.com/questions/609020/how-can-i-create-a-file-with-a-specific-size-from-a-command-line
- http://www.cyberciti.biz/faq/howto-create-lage-files-with-dd-command/
解决方案:
推荐使用fallocate命令;其次是dd命令;再次就是seq命令重定向到文件了。
2.如何创建大量测试数据?(大量小文件)
搜索关键字:http://search.aol.com/aol/search?q=Linux+how+to+make+many+small+files
参考链接:
- http://stackoverflow.com/questions/4140822/creating-multiple-files-with-content-from-shell
- http://unix.stackexchange.com/questions/120336/efficient-way-to-create-multiple-files
- http://heatware.net/linux-unix/create-large-many-number-files-thousands-millions/
解决方案:
One command to create 26 empty files:
$ touch {a..z}.txt
or 152:
$ touch {{a..z},{A..Z},{0..99}}.txt
A small loop to create 152 files with some contents:
for f in {a..z} {A..Z} {0..99} do echo hello > "$f.txt" done
You can do numbered files with leading zeros:
for i in {0..100} do echo hello > "File$(printf "%03d" "$i").txt" done
or, in Bash 4:
for i in {000..100} do echo hello > "File${i}.txt" done
3.一次创建多个目录
搜索关键字:http://search.aol.com/aol/search?q=linux+create+multiple+directory+at+once
参考链接:
- http://unix.stackexchange.com/questions/636/how-do-i-make-multiple-directories-at-once-in-a-directory
- http://stackoverflow.com/questions/4854277/how-do-i-make-multiple-folders-in-a-single-location-using-relative-path-to-the-l
解决方案:
$ mkdir monitor_log big_message error_log exec_log #一行指定多个即可
4.一行创建多个嵌套子目录
In Bash and other shells that support it, you can do:
$ mkdir ~/Labs/lab4a/folder{1..3}
or
$ mkdir ~/Labs/lab4a/folder{1,2,3}
Use shell expansion:
$ mkdir Labs/lab4a/{folder1,myfolder,foofolder}
5.Bash脚本错误“unary operator expected”
搜索关键字:http://search.aol.com/aol/search?q=unary+operator+expected
参考链接:
- http://stackoverflow.com/questions/408975/compare-integer-in-bash-unary-operator-expected
- http://stackoverflow.com/questions/13617843/unary-operator-expected
解决方案:
It doesn’t give me that error. Nonetheless, if you’re using bash it’s better to use the builtin [[ than the test ([) command.
if [[ $i -ge 2 ]]
即,推荐使用“[[”而不是“[”,因为不知道什么时候错误就出现了。
6.Linux的ps命令aux选项说明
搜索/参考链接:
- http://search.aol.com/aol/search?q=linux+ps+aux+format
- http://www.binarytides.com/linux-ps-command/
- http://superuser.com/questions/117913/ps-aux-output-meaning
7.Linux下查看目录的大小
搜索/参考链接:
- http://stackoverflow.com/questions/6644722/check-total-file-size-of-a-specific-directory-for-a-specific-user
- http://www.cyberciti.biz/faq/checking-directory-size-in-linux-command/
- http://www.cyberciti.biz/faq/how-do-i-find-the-largest-filesdirectories-on-a-linuxunixbsd-filesystem/
8.Bash中变量的连接
搜索/参考链接:
- http://search.aol.com/aol/search?q=bash+concatenate+variable
- http://stackoverflow.com/questions/4181703/how-can-i-concatenate-string-variables-in-bash
- http://stackoverflow.com/questions/14325722/bash-variables-concatenation
使用’{}’来连接普通字符串和变量。