Advertisement
Guest User

Does elegance = performance ?

a guest
Apr 4th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.79 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. if [ $# -gt 0 ];
  4. then
  5.         directory="$1"
  6.         shift 1
  7. else
  8.         printf "usage: %s directory|file ['*.ext']\n" "$(basename "$0")" >&2
  9.         exit 1
  10. fi
  11.  
  12. if [ $# -lt 1 ];
  13. then
  14.         extensions="*"
  15. else
  16.         extensions="$1"
  17. fi
  18.  
  19. if [ -d "${directory}" ];
  20. then
  21.         find "${directory}" -name "${extensions}" -type f \
  22.                 -exec wc -l {} \; \
  23.                 | awk '{ SUM += $0 } END { print SUM }'
  24. else
  25.         wc -l "${directory}" | cut -d' ' -f 1
  26. fi
  27.  
  28.  
  29. ...
  30.  
  31.  
  32. #!/bin/sh
  33.  
  34. ([ $# -gt 0 ] && [ -d "$1" ] && [ -n "$2" ] && \
  35. for i in $(ls -1 "$1"/*.$2); do
  36.         a=$(($a+$(wc -l < "$i")))
  37.         #or a+=$(wc -l < "$i") if you use bash lol
  38. done && echo $a) || printf "Usage: %s <an existing directory> <a file extension>\n" "$0" >&2 && exit 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement