rusefi_documentation/wiki-tools/brokenlinks.sh

56 lines
1.6 KiB
Bash
Raw Normal View History

2021-02-15 18:21:10 -08:00
#!/usr/bin/env bash
escape() {
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1";
}
export -f escape
escapeReplace() {
sed 's/[&/\]/\\&/g' <<<"$1";
}
export -f escapeReplace
searchfile() {
2021-02-15 19:40:53 -08:00
while IFS= read -r -u 3 link; do
2021-02-15 18:21:10 -08:00
if echo $link | grep -E '^http' >/dev/null; then
continue
fi
2021-02-15 18:52:31 -08:00
if [ $(find . -name "$link"".md" 2>/dev/null | wc -l) -gt 0 ]; then
2021-02-15 18:21:10 -08:00
continue
fi
2021-02-15 18:52:31 -08:00
if echo "$link" | grep "#" >/dev/null; then
continue
fi
if echo "$link" | grep -Ev ".md$" >/dev/null; then
if ls "$link" 2>/dev/null >/dev/null; then
continue
else
echo $link
2021-02-15 19:57:59 -08:00
FILES=$(find . -iname "*$(basename "$link")*")
2021-02-15 19:47:06 -08:00
if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then
echo "Could not find"
continue
fi
2021-02-15 18:52:31 -08:00
echo "$FILES" | cat --number
read PICK
FILE=$(echo "$FILES" | head -n $PICK | tail -n 1)
REPLACE=$(escape '('$link')')
REPLACEWITH=$(escapeReplace "$FILE")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
continue
fi
fi
2021-02-15 18:21:10 -08:00
echo $link
2021-02-15 19:57:59 -08:00
FILES=$(find . -iname "*$(basename "$link")*")
2021-02-15 19:47:06 -08:00
if [ $(echo -n "$FILES" | wc -c) -lt 1 ]; then
echo "Could not find"
continue
fi
2021-02-15 18:21:10 -08:00
echo "$FILES" | cat --number
read PICK
2021-02-15 18:52:31 -08:00
FILE=$(basename "$(echo "$FILES" | head -n $PICK | tail -n 1)" .md)
REPLACE=$(escape '('$link')')
REPLACEWITH=$(escapeReplace "$FILE")
sed -i "s/$REPLACE/\($REPLACEWITH\)/" "$1"
2021-02-15 19:40:53 -08:00
done 3< <(grep -oP '(?<=\]\().+?(?=[\)])' "$1")
2021-02-15 18:21:10 -08:00
}
export -f searchfile
find . -iname "*.md" -exec bash -c 'searchfile "$0"' {} \;