Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to display commands while perl script executing

1,379 views
Skip to first unread message

Sunita....@emc.com

unread,
May 15, 2012, 7:40:44 AM5/15/12
to begi...@perl.org
Hi

I want to print the command during script execution .
Example :


===================
$ls = `ls`;

Print "$ls\n";
==================

In the above script I want to print "ls" command before 'ls' command gets executed . Like "set -x" does in shell scripts .

Could you please help me on this ?

Thanks
Sunita


CloudWebDNS.com

unread,
May 15, 2012, 7:57:20 AM5/15/12
to begi...@perl.org
Hello,

Try this:

$ perl -le '$c="ls -l";print $c;system $c'
ls -l
total 16
drwxr-xr-x 4 pyh pyh 4096 2012-05-12 17:08 backup
drwxr-xr-x 2 pyh pyh 4096 2012-05-13 08:29 bin
drwxr-xr-x 5 pyh pyh 4096 2012-05-03 11:03 ipdata
drwxr-xr-x 4 pyh pyh 4096 2012-05-14 10:34 tmp

Sunita....@emc.com

unread,
May 15, 2012, 7:59:38 AM5/15/12
to sup...@cloudwebdns.com, begi...@perl.org
It is working for one line program . How can we implement in a script ?

-Sunita
--
To unsubscribe, e-mail: beginners-...@perl.org
For additional commands, e-mail: beginne...@perl.org
http://learn.perl.org/



Shlomi Fish

unread,
May 15, 2012, 8:16:01 AM5/15/12
to Sunita....@emc.com, begi...@perl.org
Hi Sunita,

On Tue, 15 May 2012 07:40:44 -0400
<Sunita....@emc.com> wrote:

> Hi
>
> I want to print the command during script execution .
> Example :
>
>
> ===================
> $ls = `ls`;
>
> Print "$ls\n";
> ==================
>

I should note that trapping the output of "ls" is pretty silly because there
are built-in routines and CPAN modules for doing that which are more portable
and less error-prone than piping to the shell for input.

You also want "print" in all lower-case instead of "Print" with an uppercase
"P".

> In the above script I want to print "ls" command before 'ls' command gets executed . Like "set -x" does in shell scripts .
>
> Could you please help me on this ?
>

You can do it for each and every Perl statement using
https://metacpan.org/module/Devel::TraceVars or maybe using https://metacpan.org/module/Devel::Trace .
If you want to do it only for shell commands executed using backticks, then you
can search for "qx" in http://perldoc.perl.org/overload.html (and you can also
try setting a shell environment variable to add "-x" to all shell-executed
commands).

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

You name it — COBOL does not have it.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Michael Rasmussen

unread,
May 15, 2012, 8:45:22 AM5/15/12
to Sunita....@emc.com, begi...@perl.org
On Tue, May 15, 2012 at 07:40:44AM -0400, Sunita....@emc.com wrote:
> Hi
>
> I want to print the command during script execution .
> Example :
>
> ===================
> $ls = `ls`;
>
> Print "$ls\n";
> ==================

A straightforward way to do this is to run your script with the perl debugger:

perl -d script_name

When it loads you'll see something like:

michael@bivy:~$ perl -d tpl

Loading DB routines from perl5db.pl version 1.32
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(tpl:5): my %index; #### The first executable line of your script

Then press n on your keyboard to run the next command:

DB<1> n
main::(tpl:6): my @file = <DATA>;

And after that just press <ENTER> to execute each command in turn.

As you might imagine you can do other things with the debugger too.

--
Michael Rasmussen, Portland Oregon
Other Adventures: http://www.jamhome.us/ or http://westy.saunter.us/
Fortune Cookie Fortune du courrier:
Happiness is a path, not a destination.
~ Tay Gillespie by way of Mark Canizaro

Bob McConnell

unread,
May 15, 2012, 8:53:36 AM5/15/12
to begi...@perl.org
> From: Sunita.Pradhan
Are you looking for a Perl script, or just a shell script? Using Perl for this appears to be like using a baseball bat to swat a fly. But here in a Perl mailing list, you are going to get Perl scripts. If that is not what you are looking for, you need to find a mailing list for your particular shell, which you didn't name.

Bob McConnell

Torqued

unread,
May 15, 2012, 11:04:58 AM5/15/12
to Bob McConnell, begi...@perl.org


Regards.../om

On 15-May-2012, at 18:23, Bob McConnell <r...@CBORD.com> wrote:

>> From: Sunita.Pradhan
>>
>> I want to print the command during script execution .
>> Example :
>>
>>
>> ===================
>> $ls = `ls`;
>>
>> Print "$ls\n";
>> ==================
>>
>> In the above script I want to print "ls" command before 'ls' command gets
>> executed . Like "set -x" does in shell scripts .
>>
>> Could you please help me on this ?
>
It looks to me she is simply looking for a debugger, set -x in shell helps in debugging the scripts, for perl the best option would be to use 'perl -d script name' option.

> Are you looking for a Perl script, or just a shell script? Using Perl for this appears to be like using a baseball bat to swat a fly. But here in a Perl mailing list, you are going to get Perl scripts. If that is not what you are looking for, you need to find a mailing list for your particular shell, which you didn't name.
>
> Bob McConnell
>
>
0 new messages