LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Writing string to file (https://www.linuxquestions.org/questions/linux-newbie-8/writing-string-to-file-4175510163/)

battles 07-04-2014 08:19 PM

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

mreff555 07-04-2014 08:35 PM

It doesn't on my machine.
Are you sure you aren't just referring to the line which your are typing?

battles 07-04-2014 08:42 PM

Quote:

Originally Posted by mreff555 (Post 5198854)
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.

jpollard 07-05-2014 04:19 AM

Is it possible you have gotten the verbose option set on your shell? (could you show a recorded sequence that shows the problem?)

mreff555 07-05-2014 06:13 AM

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

It appears to be a similar problem.

battles 07-05-2014 07:06 AM

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

jpollard 07-05-2014 01:29 PM

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.

battles 07-05-2014 01:47 PM

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.

jpollard 07-05-2014 02:34 PM

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.

battles 07-05-2014 02:43 PM

I'm getting there. I have made an abbreviated notebook and am just now getting where I need to manipulate data.


All times are GMT -5. The time now is 07:38 PM.