Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash cgi won't return image

Tags:

bash

cgi

perl

rrd

We have a monitoring system making RRD databases. I am looking for the most light way of creating graphs from this RRD files for our HTML pages. So I don't want to store them in files. I am trying to create simple BASH CGI script, that will output image data, so I can do something like this:

<img src="/cgi-bin/graph.cgi?param1=abc"></img>

First of all, I am trying to create simple CGI script, that will send me PNG image. This doesn't work:

#!/bin/bash
echo -e "Content-type: image/png\n\n"
cat image.png

But when I rewrite this to PERL, it does work:

#!/usr/bin/perl
print "Content-type: image/png\n\n";
open(IMG, "image.png");
print while <IMG>;
close(IMG);
exit 0;

What is the difference? I would really like to do this in BASH. Thank you.

like image 510
Petr Cézar Avatar asked Apr 15 '14 11:04

Petr Cézar


1 Answers

Absence of -n switch outputs third newline, so it should be

echo -ne "Content-type: image/png\n\n"

or

echo -e "Content-type: image/png\n"

from man echo

  -n     do not output the trailing newline
like image 175
mpapec Avatar answered Nov 15 '22 09:11

mpapec