Hi Monks,

I'm new to perl and learning to develop my first Perl-CGI HTML page. I have a bash script that run few of the database functions within our organization, part of the script while performing different operations it does show a progress bar displaying (.)dots on the terminal

There is a requirement and now we want the script to be run from HTML page wherein our developers could use the webpage to run the script without actually connecting to the server. After investigating found Perl-CGI is a good option and Initially I had problem with perl based buffering while running the script through perl-CGI. However I fixed the issue following suggestions from other posts by using ($|=1;) to generate un buffered perl output.

After fixing the buffer issue, now I have 2 issues. please see below,

1) while I execute the perl script from terminal the output does display (.)dots, However they appear in wrong order....

2) If I execute from the webpage - output doesn't display (.)dots - however waits until it finishes the function + wrong order In order to help debugging the script - I made sample BASH and PERL to re-generate the same issue, I am attaching the BASH and Perl code here. Please see and advise if we can achieve OUTPUT displaying the progress bar and also in the correct order

Bash_Script: Name: shell_script.sh
#!/bin/bash ### rundots() { ( trap 'exit 0' SIGUSR1; while : ; do echo -ne '.' >&2; +sleep 0.2; done) & dotpid=$!; } ### stopdots() { kill -USR1 $dotpid; wait $dotpid; trap EXIT; } ### startdots() { echo -e "\nExecuting function >>>> $(tr [a-z] [A-Z] <<< "${FUNCNAME[ 1 + ]}") <<<<<" echo -ne "---->>Script executing, please wait" rundots; trap "stopdots" EXIT; return 0; sleep 0.3} ### main1() { startdots echo -e "display space usage" > /tmp/shell_script.log sleep 4 du -sh * >> /tmp/shell_script.log stopdots } main2() { startdots echo -e "list files," >> /tmp/shell_script.log sleep 4 ls -lsrt >> /tmp/shell_script.log stopdots echo -e "\n\n*********** END OF THE BASH SCRIPT ************" } ### call functions main1 main2
Perl-CGI: Name: perl_script.pl
#!/usr/bin/perl -w $|=1; use CGI qw(:standard); #use IO::Handle; use FileHandle; print "Content-type: text/html\n\n"; print '<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Page title</title> <link rel="stylesheet" href="hari.css" type="text/css" media=" +screen"> </head>'; open (my $cmds, "/var/www/cgi-bin/shell_script.sh |"); open (my $LOG, '>', '/tmp/perl.log'); select((select($LOG), $|=1)[0]); while (<$cmds>) { #print "<pre>"; print; print $LOG $_; #print "</pre>"; } close $cmds; close $LOG; print ' </body> </html>';
Thanks in advance.

In reply to Perl CGI + Bash script output in wrong order by h.chris

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.