Last active
August 27, 2021 16:01
-
-
Save johnloy/4cb8e76a8614448cff4ac382f52adc5d to your computer and use it in GitHub Desktop.
This file contains 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
append_line() { | |
set -e | |
local update line file pat lno | |
update="$1" | |
line="$2" | |
file="$3" | |
pat="${4:-}" | |
lno="" | |
echo "Update $file:" | |
echo " - $line" | |
if [ -f "$file" ]; then | |
if [ $# -lt 4 ]; then | |
lno=$(\grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ') | |
else | |
lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ') | |
fi | |
fi | |
if [ -n "$lno" ]; then | |
echo " - Already exists: line #$lno" | |
else | |
if [ $update -eq 1 ]; then | |
[ -f "$file" ] && echo >> "$file" | |
echo "$line" >> "$file" | |
echo " + Added" | |
else | |
echo " ~ Skipped" | |
fi | |
fi | |
echo | |
set +e | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment