LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with my bash script (https://www.linuxquestions.org/questions/programming-9/problem-with-my-bash-script-4175484371/)

flypadre 11-12-2013 04:40 PM

Problem with my bash script
 
I have a script in which some html code is created with information about my system. What I would like is to have the html code automatically written to a file.
Here is the script
Code:

#!/bin/bash
# make_page - A script to produce an HTML file

##### Constants

TITLE="My System Information for $HOSTNAME"
RIGHT_NOW=$(date +"%x %r %Z")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"

##### Functions

function system_info
{
        echo "<h2>System release info</h2>"
        echo "<p>Function not yet implremented</p>"
}

function show_uptime
{
        echo "<h2>System uptime</h2>"
        echo "<pre>"
        uptime
        echo "</pre>"
}

function drive_space
{
        echo "<h2>Filesystem space</h2>"
        echo "<pre>"
        df
        echo "</pre>"
}

function home_space
{
        # Only the superuser can get this information

        if [ "$(id -u)" = "0" ]; then
                echo "<h2>Home directory space by user</h2>"
                echo "<pre>"
                echo "Bytes Directory"
                du -s /home/* | sort -nr
                echo "</pre>"
        fi

        # end of the home_space
}


cat <<- _EOF_
        <HTML>
        <HEAD>
                <TITLE>
                $TITLE
                </TITLE>
        </HEAD>

        <BODY>
                <H1>$TITLE </H1>
                <P>$TIME_STAMP</P>
                $(system_info)
                $(show_uptime)
                $(drive_space)
                $(home_space)
        </BODY>
        </HTML>
_EOF_

I tried putting the section of the script that contains the html code starting at "cat <<- all the way to _EOF_" in a function first and then calling that function to write it's contents into a file and when I couldn't get that to work I tried making the html code a variable and then writing that into a file and was not successful.

I can do what I want outside of the script: make_page > page.html
I just can't do it within the script.

kinneyd 11-12-2013 06:19 PM

If you're trying to have your script write this to a file "page.html" rather than having to re-direct the output of the script. Try the following.

Code:

cat <<- _EOF_
        <HTML>
        <HEAD>
                <TITLE>
                $TITLE
                </TITLE>
        </HEAD>

        <BODY>
                <H1>$TITLE </H1>
                <P>$TIME_STAMP</P>
                $(system_info)
                $(show_uptime)
                $(drive_space)
                $(home_space)
        </BODY>
        </HTML>
_EOF_


With....
Code:

echo "
        <HTML>
        <HEAD>
                <TITLE>
                $TITLE
                </TITLE>
        </HEAD>

        <BODY>
                <H1>$TITLE </H1>
                <P>$TIME_STAMP</P>
                $(system_info)
                $(show_uptime)
                $(drive_space)
                $(home_space)
        </BODY>
        </HTML>
" > page.html


grail 11-13-2013 02:59 AM

hmmm ... that would seem to be an odd approach kinneyd when a here document is all about redirection and capturing what you want :)

@OP it generally helps if you tell us 'what' did not work about your attempts as it may be something simple :)

Here are 2 options you could use (I will only show additional code):

Option 1:
Code:

create_page()
{
        cat <<-_EOF_
        <HTML>
        <HEAD>
                <TITLE>
                $TITLE
                </TITLE>
        </HEAD>

        <BODY>
                <H1>$TITLE </H1>
                <P>$TIME_STAMP</P>
                $(system_info)
                $(show_uptime)
                $(drive_space)
                $(home_space)
        </BODY>
        </HTML>
_EOF_
}

create_page > page.html

Option 2:
Code:

create_page()
{
        cat > "$1" <<-_EOF_
        <HTML>
        <HEAD>
                <TITLE>
                $TITLE
                </TITLE>
        </HEAD>

        <BODY>
                <H1>$TITLE </H1>
                <P>$TIME_STAMP</P>
                $(system_info)
                $(show_uptime)
                $(drive_space)
                $(home_space)
        </BODY>
        </HTML>
_EOF_
}

create_page page2.html

See how that goes :)


All times are GMT -5. The time now is 05:31 PM.