Add bash pitfalls module

This commit is contained in:
Pragmatic Software 2021-05-23 17:56:40 -07:00
parent c820bd5359
commit 2cb7b6418e
2 changed files with 107 additions and 0 deletions

45
modules/bashpf.pl vendored Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/perl -w
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
my $query = "@ARGV";
print "Usage: pf <id or search text>\n" and exit 0 if not length $query;
my (%pf, $match);
open(FILE, "< bashpf.txt") or print "Can't open Bash Pitfalls: $!" and exit 1;
foreach my $line (<FILE>) {
if ($line =~ /^(\d+)\.\s+(.*)$/) {
$pf{$1} = $2;
}
}
close FILE;
if ($query =~ / >/) {
$rcpt = $query;
$query =~ s/ +>.*$//;
$rcpt =~ s/^.* > *//;
}
if (exists $pf{$query}) {
$match = $query;
} else {
foreach my $key (keys %pf) {
if ($pf{$key} =~ /$query/i) {
$match = $key;
last;
}
}
}
if ($match) {
my $id = "pf$match";
print "$rcpt: " if $rcpt;
print "https://mywiki.wooledge.org/BashPitfalls#$id -- Don't do this! -- $pf{$match}\n";
} else {
print "No matches found at https://mywiki.wooledge.org/BashPitfalls\n";
}

62
modules/bashpf.txt vendored Normal file
View File

@ -0,0 +1,62 @@
1. for f 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 var=$(cmd)
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
45. y=$(( array[$x] ))
46. read num; echo $((num+1))
47. IFS=, read -ra fields <<< "$csv_line"
48. export CDPATH=.:~/myProject
49. OIFS="$IFS"; ...; IFS="$OIFS"
50. hosts=( $(aws ...) )
51. Non-atomic writes with xargs -P
52. find . -exec sh -c 'echo {}' \;
53. sudo mycmd > /myfile
54. sudo ls /foo/*
55. myprogram 2>&-
56. Using xargs without -0
57. unset a[0]
58. month=$(date +%m); day=$(date +%d)
59. i=$(( 10#$i ))
60. set -euo pipefail
61. [[ -v hash[$key] ]]
62. (( hash[$key]++ ))