Last active
September 2, 2017 15:36
-
-
Save aleclarson/99c8eaa1de542c530f9af7112cb603fb to your computer and use it in GitHub Desktop.
Read a .gitignore-like file with Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
read_lines() { | |
while IFS='' read -r line || [[ -n $line ]]; do | |
if [ -z "${line// }" ] || [[ $line == "#"* ]]; then continue; fi | |
echo "$line" | |
done < "$1" | |
} | |
if [ -f "$1" ]; then read_lines "$@"; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IFS=''
prevents leading/trailing whitespace from being trimmed[[ -n $line ]]
prevents the last line from being ignored if it doesn't end with a\n
[ -z "${line// }" ]
trims whitespace before checking if the string is empty[[ $line == "#"* ]]
looks for strings beginning with#