BASH脚本陷阱

请务必注明作者并给出原始链接出处。
回复
头像
acheng
锌 Zn
帖子: 581
注册时间: 2011-07-07 21:52

BASH脚本陷阱

帖子 acheng » 2013-12-07 21:15

BASH脚本编程中一些常见而又不太容易察觉到的错误。
你能看出下面的脚本用法中有什么问题吗?
1. for i in $(ls *.mp3)
2. cp $file $target
3. Filenames with leading dashes
4. [ $foo = "bar" ]
5. cd $(dirname "$f")
6. [ "$foo" = bar && "$bar" = foo ]
7. [[ $foo > 7 ]]
8. grep foo bar | while read -r; do ((count++)); done
9. if [grep foo myfile]
10. if [bar="$foo"]; then ...
11. if [ [ a = b ] && [ c = d ] ]; then ...
12. read $foo
13. cat file | sed s/foo/bar/ > file
14. echo $foo
15. $foo=bar
16. foo = bar
17. echo <<EOF
18. su -c 'some command'
19. cd /foo; bar
20. [ bar == "$foo" ]
21. for i in {1..10}; do ./something &; done
22. cmd1 && cmd2 || cmd3
23. echo "Hello World!"
24. for arg in $*
25. function foo()
26. echo "~"
27. local varname=$(command)
28. export foo=~/bar
29. sed 's/$foo/good bye/'
30. tr [A-Z] [a-z]
31. ps ax | grep gedit
32. printf "$foo"
33. for i in {1..$n}
34. if [[ $foo = $bar ]] (depending on intent)
35. if [[ $foo =~ 'some RE' ]]
36. [ -n $foo ] or [ -z $foo ]
37. [[ -e "$broken_symlink" ]] returns 1 even though $broken_symlink exists
38. ed file <<<"g/d\{0,3\}/s//e/g" fails
39. expr sub-string fails for "match"
40. On UTF-8 and Byte-Order Marks (BOM)
41. content=$(<file)
42. for file in ./* ; do if [[ $file != *.* ]]
43. somecmd 2>&1 >>logfile
44. cmd; (( ! $? )) || die
http://mywiki.wooledge.org/BashPitfalls

回复

在线用户

正浏览此版面之用户: 没有注册用户 和 3 访客