Advertisement
Guest User

Untitled

a guest
Jun 14th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.20 KB | None | 0 0
  1. #!/bin/bash
  2. # When ${@} =~ ${COMMENTREGEX} is tested, it appears to loop for each match?
  3.  
  4. ignore_comments(){
  5.     if [ -z "${COMMENTREGEX}" ]; then
  6.         COMMENTREGEX='^\s*#.*'
  7.     fi
  8.     echo "Whole line after: ${@}"
  9.     if [[ ${@} =~ ${COMMENTREGEX} ]]; then
  10.         printf "Ignoring: %s, commented out\n" ${@}
  11.     else
  12.         return 1
  13.     fi
  14. }
  15.  
  16. while IFS='' read -r line || [[ -n "${line}" ]]; do
  17.     echo "Whole line before: ${line}"
  18.     if ignore_comments "${line}"; then
  19.             echo "${line}"
  20.     fi
  21. done < /export/home/alfie/Documents/scripts/bash/test_file
  22.  
  23. # test_file
  24. line1 word2 word3 word4
  25. # This is a comment
  26. #another
  27. line2
  28. line3
  29.  
  30. # Output ./test_loop
  31. Whole line before: line1 word2 word3 word4
  32. Whole line after: line1 word2 word3 word4
  33. Whole line before: # This is a comment
  34. Whole line after: # This is a comment
  35. Ignoring: #, commented out
  36. Ignoring: This, commented out
  37. Ignoring: is, commented out
  38. Ignoring: a, commented out
  39. Ignoring: comment, commented out
  40. # This is a comment
  41. Whole line before: #another
  42. Whole line after: #another
  43. Ignoring: #another, commented out
  44. #another
  45. Whole line before: line2
  46. Whole line after: line2
  47. Whole line before: line3
  48. Whole line after: line3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement