LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   need help creating a webpage with bashscript (https://www.linuxquestions.org/questions/linux-general-1/need-help-creating-a-webpage-with-bashscript-4175502887/)

mia_tech 04-24-2014 05:03 PM

need help creating a webpage with bashscript
 
I have a directory containing documentation like: php, mysql and others. So I'm working on a bash script that basically runs
Code:

grep -ir searchpattern directory
and I'm getting the output and putting it into a webpage. but I'm having trouble formatting the output because the browser open local files in the for of
Code:

file:///path.html
I'm not able to find a way to append file:///path to the result from grep

Code:

searchTerm=$2        #search term eg: parameters, variables, etc..
directory=$1        #directory to be searched eg: php, mysql, bash, etc
prefix="file://$(pwd)"        #prefix to be added to every result in order to open in browser
results=""        #storing result from search


#1-check for right num of parameters
if [ $# -ne 2 ]; then
        echo "USAGE $(basename $0) directory searchterm"
        exit 1
fi

webpage() {
       
        cat << END
                <!doctype html>
                <html>
                <head><title>SearchDocs</title></head>
                <body>
                $results
                </body>
                </html>
END
}


#2-loop through parameters and and search
results=$(grep -ir $searchTerm $directory | cut -d: -f1 | sort -u)

for line in $results
do
        results="${results}$prefix/$line"
done

webpage $results > searchdoc.html

I would eventually pass the result as links <a> tags to the page... so if anyone has an idea how to do it I appreciate it

Firerat 04-24-2014 06:18 PM

Look at awk

or gawk,

awk can be confusing, since a few versions of it
Figure out which you have and run...

If awk is not an option, grep + sed

Perl and python also options


All times are GMT -5. The time now is 08:12 AM.