From 2cb7b6418ef3c6859055f8f6c1024ff9fc009193 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 23 May 2021 17:56:40 -0700 Subject: [PATCH] Add bash pitfalls module --- modules/bashpf.pl | 45 +++++++++++++++++++++++++++++++++ modules/bashpf.txt | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100755 modules/bashpf.pl create mode 100644 modules/bashpf.txt diff --git a/modules/bashpf.pl b/modules/bashpf.pl new file mode 100755 index 00000000..8454daa8 --- /dev/null +++ b/modules/bashpf.pl @@ -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 \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 () { + 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"; +} diff --git a/modules/bashpf.txt b/modules/bashpf.txt new file mode 100644 index 00000000..31c3a565 --- /dev/null +++ b/modules/bashpf.txt @@ -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 <&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]++ ))