LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2014, 08:19 PM   #1
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Rep: Reputation: Disabled
Writing string to file


Can't get rid of echo to console. When I do this:

echo "<br>" >> /etc/temp.txt

Is echos to the console also. Is there a way to stop this?

This also sends a message to the console:

lsb_release -a >> /etc/temp.txt

Any way to stop this?

Thanks
 
Old 07-04-2014, 08:35 PM   #2
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 473

Rep: Reputation: Disabled
It doesn't on my machine.
Are you sure you aren't just referring to the line which your are typing?
 
Old 07-04-2014, 08:42 PM   #3
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by mreff555 View Post
It doesn't on my machine.
Are you sure you aren't just referring to the line which your are typing?
No, these lines are in a script and not only write out to the file, but print to the screen also, upon doing a ./scriptname. I would think that they would only write to the file. If I type them into the console, they don't print to the screen, only when executed through the script.

Last edited by battles; 07-04-2014 at 08:44 PM.
 
Old 07-05-2014, 04:19 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Is it possible you have gotten the verbose option set on your shell? (could you show a recorded sequence that shows the problem?)
 
Old 07-05-2014, 06:13 AM   #5
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 473

Rep: Reputation: Disabled
Oh, try this:
http://www.frihost.com/forums/vt-66403.html

It appears to be a similar problem.
 
Old 07-05-2014, 07:06 AM   #6
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
Here is the code that does this:

#!/bin/bash
#
# Script to build server statistics Lstats.html
#
varupt=$(uptime)
serverid="test.net - "
webpgnm="Lstats.html"
tempvar=""
tempnum=0
fontsz="2"
hfontsz="2"

cat /dev/null > /var/www/$webpgnm # clear html file

echo "<html><head><title>Server Stats</title></head><body><body bgcolor="#E7DFAD" TEXT="#000040" LANG="en-US" DIR="LTR">" > /var/www/$webpg$

#date
echo "<br><font face=\"Verdana\" size=$fontsz>&nbsp;" >> /var/www/$webpgnm
echo $serverid $(date) >> /var/www/$webpgnm
echo "</font><br><br>" >> /var/www/$webpgnm

# machine
echo "<font face=\"Verdana\" size=$hfontsz><caption>&nbsp;Machine</caption></font>" >> /var/www/$webpgnm
echo "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\"><tr><td><font face=\"Courier New\" size=$fontsz>" >> /var/www/$webpgnm
uname -mrs > /etc/temp.txt
echo "<br>" >> /etc/temp.txt
lsb_release -a >> /etc/temp.txt
sed '/No LSB/d' /etc/temp.txt
sed 's/Description:/<br>Description:/g' /etc/temp.txt > /etc/temp2.txt
sed 's/Release:/<br>Release:/g' /etc/temp2.txt > /etc/temp.txt
sed 's/Codename:/<br>Codename:/g' /etc/temp.txt > /etc/temp2.txt
cat /etc/temp2.txt >> /var/www/$webpgnm
varupt=`echo ${varupt//up/Up}`
varupt=`echo ${varupt//load/Load}`
awk -F '[ \t\n\v\r]' '{print "<br>"$2" "$3" "$4" "$5" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15}' <<< $varupt >> /var/www/$webpgnm
echo "</font></td></tr></table><br>" >> /var/www/$webpgnm # end build Machine
echo "</font></td></tr></table><br>" >> /var/www/$webpgnm

echo "</body></html>" >> /var/www/$webpgnm

exit 0

This is what it prints to screen with a ./pgmname

No LSB modules are available.
Linux 3.2.0-4-686-pae i686
<br>
Distributor ID: Debian
Description: Debian GNU/Linux 7.5 (wheezy)
Release: 7.5
Codename: wheezy

Last edited by battles; 07-05-2014 at 07:07 AM.
 
Old 07-05-2014, 01:29 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Overly complex...

Three errors:

1) Line 15. I believe $webpg$ should be $webpgnm
2) Line 15. You have "<body><body ...". There should be only one. There are some other HTML errors too.
3) Line 28. you use a "sed" with no output file specification - thus the output is sent to the terminal.

I say overly complex because you are using multiple echo commands for what could be more easily done with a "hereis" input.

Here is an example of a cleaned up version:

Code:
#!/bin/bash
#
# Script to build server statistics Lstats.html
#
varupt=$(uptime)
serverid="test.net - "
webpgnm="Lstats.html"
tempvar=""
tempnum=0
fontsz="2"
hfontsz="2"

cat >$webpgnm <<-EOF
	<html>
	<head>
	<title>Server Stats</title>
	</head>
	<body bgcolor="#E7DFAD" TEXT="#000040" LANG="en-US" DIR="LTR">
	<br><font face="Verdana" size=$fontsz>&nbsp;
	$serverid $(date)
	</font><br><br>
	<font face="Verdana" size=$hfontsz><caption>&nbsp;Machine</caption></font>
	<table border="1" cellpadding="5" cellspacing="0">
	<tr><td><font face=\"Courier New\" size=$fontsz>
EOF
echo "$(uname -mrs)<br>" >>$webpgnm

lsb_release -a |
   sed '/No LSB/d' |
   sed 's/Description:/<br>Description:/g' |
   sed 's/Release:/<br>Release:/g' |
   sed 's/Codename:/<br>Codename:/g' >>$webpgnm

varupt=${varupt//up/Up}
varupt=${varupt//load/Load}

# this works, but is a bit awkward
# awk -F '[ \t\n\v\r]' '{print "<br>"$2" "$3" "$4" "$5" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15}' <<< $varupt >> $webpgnm

# this version is a bit simpler, though you have to use the {} around two digit parameters
# (the "<br>$2" is to exclude a space between the <br> and the parameter value)

set $varupt
echo "<br>$2" $3 $4 $5 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} >>$webpgnm

cat >>$webpgnm <<-EOF
	</font></td></tr></table><br>
	</font></td></tr></table><br>
	</body></html>"
EOF

exit 0
This eliminates the scratch file you were creating in /etc (bad place - /etc is for configuration files)
It also eliminates the HTML errors.

I believe the piped sequence of sed commands can also be eliminated, but will admit the sequence is simpler to debug and read.

I think there is a better way to handle the $varupt stuff. This just seems a bit awkward. But you can't beat what really works.

BTW, if you are going to use variables to hold file names... it is clearer to put the entire path in the variable too. It would eliminate multiple substitutions later on.

One final thing... This script is most likely being run as root. This means that the output file is USUALLY owned by root and with roots permissions, and security label. This shouldn't be a problem once the file is created and given the correct ownership/permissions/security label (RH and Fedora servers). The script preserves that when run by root as it only truncates the file at the beginning, then appends to it.

Last edited by jpollard; 07-05-2014 at 01:34 PM.
 
Old 07-05-2014, 01:47 PM   #8
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
Thanks. I like the lsb_release -a better also. Still learning this stuff.

The $webpg$ was a result of a copy from terminal error.

I noticed the double body statements and fix that just before seeing your post.

I am used to throwing things back upon them self in other languages I use, so did it here also.

Bash is kind of like IBM assembler, only much more syntax prone.

Thanks again.
 
Old 07-05-2014, 02:34 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
I kind of thought the $webpg$ was a copying error.

Bash is much higher level than IBM assembler. The usual problem is getting the quotes right - especially when doing multiple levels of substitution. Handling the substitions is much more like a macro processor, but with entire file manipulation as the underlying operations.
 
Old 07-05-2014, 02:43 PM   #10
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
I'm getting there. I have made an abbreviated notebook and am just now getting where I need to manipulate data.
 
  


Reply



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] writing substituted string into a file using PERL rohitchauhan Programming 4 09-18-2013 01:24 AM
Need help on writing a string into a file in a particular position ajaygowni Programming 4 07-28-2012 08:12 AM
Need help on writing a string into a file in a particular position ajaygowni Linux - General 3 07-28-2012 06:53 AM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration