Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have just begun writing scripts in bash and I am putting some of them on my web server (whihc is located on my virtual Debian 6.0.4 machine).

I have installed lighttpd on this machine.and when I execute for instance this command :

TOP=`top -n 1`;
PS=`ps aux`;

echo "that's top\n" $TOP;
echo "ps\n" $PS;

Nothing is printed as $TOP and for $PS the text is written without separation for lines (all is written in a single line). How can I change this ?

share|improve this question
    
Is this supposed to be a CGI script through lighttpd? –  Arcege Feb 19 '12 at 12:46
    
@Arcege Yes it is. –  Patryk Feb 19 '12 at 12:50

3 Answers 3

up vote 3 down vote accepted

The output of plain top is not suitable for displaying on anything but a terminal. You should use the -b switch to get plain text output.

And the end of line characters are going to get stripped (replaced by spaces) by your shell with what you're doing - there's no reason for you to store those outputs in variables, you could simply run those commands when you need them.

I'm assuming you're returning the data to a web browser. If that is indeed the case, you should do some minimal HTML formatting if you want a chance at having the output displayed correctly.

Try something like:

#! /bin/sh
echo -e "Content-type: text/html\n"
echo "<html><body>"
echo "<h1>top</h1>"
echo "<pre>"
top -b -n 1
echo "</pre>"
echo "<h1>ps</h1>"
echo "<pre>"
ps aux
echo "</pre>"
echo "</body></html>"
share|improve this answer
    
Can you tell me why I cannot use this top -b -n 1 > /home/username/t.txt ? nothing is created in the home dir –  Patryk Feb 19 '12 at 13:45
    
Probably permission issues. No way to tell really without an error message. –  Mat Feb 19 '12 at 13:47
2  
Actually, the real problem (re: munged output) is that $TOP must be written as "$TOP" to preserve the spaces. Welcome to Bourne Shell. –  hhaamu Feb 19 '12 at 20:48

The reason that your output is not display is because you are not following the CGI protocol. You must send server response headers before any other output.

TOP=`top -n 1`;
PS=`ps aux`;

echo "Content-Type: text/plain"  # response header
echo ""                          # end of headers
echo "that's top\n" $TOP;
echo "ps\n" $PS;

Use text/plain will give the formatting you want, but will not give the HTML usage like hyperlinks. For that, use:

TOP=`top -n 1`
PS=`ps aux`

echo "Content-Type: text/html"
echo ""
echo "<html>"
echo "<head><title>command output</title></head>"
echo "<body>that's top<br/>"
echo "<pre>$TOP</pre>"
echo "<br/>ps<br/>"
echo "<pre>$PS</pre>"
echo "</body>"
share|improve this answer

If this was run in a terminal, when you echo them you need to put quotes around the variable to keep the newline characters.

TOP=`top -b -n 1`;
PS=`ps aux`;

echo "that's top";
echo "$TOP";
echo "ps";
echo "$PS";

If the output gets sent to a webpage, then you additionally need the pre tags

TOP=`top -b -n 1`;
PS=`ps aux`;
echo "<pre>";
echo "that's top";
echo "$TOP";
echo "ps";
echo "$PS";
echo "</pre>";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.