I am attempting to output a remote bash file onto a page so the user can see what is happening. I am able to read the bash file, however, anything I try it isn't adding a newline between each echo statements. Does anyone know how to do this?
Here is my bash file:
scirpt.sh
#!/bin/bash
echo 'touch /tmp/testfile'."\n"
echo "I am up\n"
echo '\n'
echo "\n"
echo
echo 'hello there'
Here is my php logic:
main.php
if(isset($_POST['option']) && $_POST['option'] == 1) {
$stream = ssh2_exec($connection, "/tmp/user/testscripts/up.sh");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$output = stream_get_contents($stream_out);
}
....
....
....
<div class="box1">
<form method="post">
<label class="col">Up/Down</label>
<span class="col">
<input type="radio" name="option" id="r1" value="1" />
<label for="r1">Up</label>
<input type="radio" name="option" id="r2" value="2" />
<label for="r2">Down</label>
</span>
<span class="col">
<input type="submit" class="button"/>
</span>
</form>
<?php
echo "<pre>$output</pre>"
?>
Here is my output on the page:
touch /tmp/testfile.\\n I am in up\\n \\n \\n hello there
When you echo something you'll get a literal version of it. C-style \\n
isn't an option.
Two options. The first is:
echo 'touch /tmp/testfile'
echo 'I am up'
echo
echo
echo
echo 'hello there'
Section option is a HEREDOC supplied to cat
:
cat <<END
touch /tmp/testfile
I am up
hello there
END
bash echo
appends a newline unless suppressed with -n
, so don't include them. There is a -e
option to have echo
interpret escaped characters but you don't need that here.
Also, to display on a web page (HTML) you will need to wrap in <pre></pre>
tags or use the PHP nl2br()
.
So use:
echo 'touch /tmp/testfile'
echo 'I am up'
echo
echo
echo
echo 'hello there'
Or:
echo 'touch /tmp/testfile
I am up
hello there'
HTML doesn't render newlines so this treats it as pre-formatted text that will:
echo '<pre>' . stream_get_contents($stream_out) . '</pre>';
Or convert the newlines to the HTML <br>
tags:
echo nl2br(stream_get_contents($stream_out));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.