patternhtmlMinor
Bash script to extract HTML comment into a Markdown file
Viewed 0 times
scriptcommentfilebashintomarkdownextracthtml
Problem
I learned Bash a million years ago. I just wrote this simple script used to get the first lot of HTML comments from a file, and spit it out in order to create a
It is just. So. Ugly. I read bits and pieces over the years, and I am sure it can be improved so much...
Here we go:
Questions:
-
Is there a better way to do
-
Should
-
Is there a better way to preserve spaces, rather than zapping
-
Any more pearls of wisdom, other than quitting my (short lived) career of bash scripter?
README.md file.It is just. So. Ugly. I read bits and pieces over the years, and I am sure it can be improved so much...
Here we go:
#!/bin/bash
IFS=''
active='0';
cat hot-form-validator.html | while read "line";do
echo $line | grep '\-\->' > /dev/null
if [ $active = '1' -a $? = '0' ];then
exit 0;
fi;
suppress=0;
echo $line | grep '^ *@' > /dev/null
if [ $? = '0' ];then suppress='1'; fi;
if [ $active = '1' -a $suppress = '0' ];then echo $line;fi;
echo $line | grep " /dev/null
if [ $? = '0' ];then active='1'; fi;
doneQuestions:
-
Is there a better way to do
grep and then check $?? Back in the day it was the way to go, but...-
Should
active be a proper number rather than a string with a number? I know, it could be anything... but having a string that can be 0 or 1 just feels wrong.-
Is there a better way to preserve spaces, rather than zapping
IFS?-
Any more pearls of wisdom, other than quitting my (short lived) career of bash scripter?
Solution
A bug
-
If your html contains a backspace character e.g
your code interprets it a
The special character n is a way to include read line.
to avoid this, add "-r" switch to your read line
-
If your html contains a backspace character e.g
The special character \n is a way to include read lineyour code interprets it a
The special character n is a way to include read line.
to avoid this, add "-r" switch to your read line
cat hot-form-validator.html | while read -r "line";do- You want to check if your file exists before reading it
if [ -f $filename ]; then
#do something
else
#do something
fiCode Snippets
if [ -f $filename ]; then
#do something
else
#do something
fiContext
StackExchange Code Review Q#135204, answer score: 3
Revisions (0)
No revisions yet.