LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-12-2013, 04:40 PM   #1
flypadre
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 11-12-2013, 06:19 PM   #2
kinneyd
LQ Newbie
 
Registered: Oct 2011
Posts: 21

Rep: Reputation: Disabled
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
 
Old 11-13-2013, 02:59 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,517

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 01:57 AM
Problem running an Expect script within a Bash script mbeipi Programming 8 03-15-2011 04:07 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 05:39 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM


All times are GMT -5. The time now is 09:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration