CJmonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I am trying to make a command history file for my software CJ. So, I would like to get the exact command given to bash on a terminal including the quotation marks, and so on.

For instance, consider

$ perl CJ.pl run example_reduce.m sherlock -mem 8G -m "this is a test"

currently, I am doing

$cmdline = `ps -o args $$ | grep CJ.pl`;

to get the command, but the problem is bash interprets and the quotation marks are gone. So I get (in the history file I am trying to make)

12 perl CJ.pl run example_reduce.m sherlock -mem 8G -m this is a test

As you see, the quotes are deleted. The reason I need the exact command is to rerun this line of history using another routine I built. Namely, when I write

perl CJ.pl @12

I expect to reissue command number 12. But because of the deleted quotes on the message (-m option), it generates error!... Does anyone know how I can read the command as is! I have searched a lot on google, but wasn't successful in finding something clean that actually works!

Best,

CJmonk

Replies are listed 'Best First'.
Re: Catch the exact command line including the quotation marks and so on
by Corion (Patriarch) on Oct 23, 2015 at 06:07 UTC

    What's wrong with saving @ARGV at the start of your program? You won't get the parameters given to perl itself, but that shouldn't bother you IMO.

      I probably missed something, but the output in the following still drops the input (CLI) double-quotes under Win7:

      my @input = @ARGV; say @input = join(" ", @input); =head CLI and output, AS perl 5.18.4, Win 7: C:\>@argv_test.pl foo bar baz 'bat' "bitchin" foo bar baz 'bat' bitchin ^ ^ =cut

        Ah - yes, I would blindly quote everything from @ARGV, sorry.

      Hi, I don't get the desired behavior with ARGV. Here is an example:

      perl CJ.pl -help -m "double quote goes here" 'single quote goes here'

      if I do

      my $script = join " ", $0, @ARGV; print "$script \n "; die;

      I get:

      CJ.pl -help -m doublequotegoeshere singlequotegoeshere

      As you can see, not only quotes are gone, the spaces in the message is removed too! Here is the perl info I am using on Mac:

      This is perl 5, version 16, subversion 3 (v5.16.3) built for darwin-th +read-multi-2level

        I don’t think so. You’re doing something else somewhere. And why the die? Might cloud order of ouput depending on what you do with the script.

        moo@cow~>perl ab -help -m "double quote goes here" 'single quote goes +here' ab -help -m double quote goes here single quote goes here Died at ab line 3. __END__ This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-2l +evel
Re: Catch the exact command line including the quotation marks and so on
by Discipulus (Canon) on Oct 23, 2015 at 07:12 UTC
    hello, as Corion said is simple as saving or printing your @ARGV, bu expanding it further you can have a module specialized to this work, maybe you tell the module to write in some special history file.
    # HistorySaver.pm use strict; use warnings; BEGIN{ print join " ", $^X,($^C ? '-c' : ''),(${^TAINT} ? '-T':''),$^W + ? '-w' :'',$0,@ARGV; } 1; # a true value!
    Then you can use the module in all your scripts or add as command line arg:
    perl -MHistorySaver -w scripts.pl --long 43 --good kawasaki

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Catch the exact command line including the quotation marks and so on
by choroba (Cardinal) on Oct 23, 2015 at 09:42 UTC
    If you just need to keep a log of everything going on in a terminal, use script.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Catch the exact command line including the quotation marks and so on
by Anonymous Monk on Oct 23, 2015 at 05:44 UTC