how to get exact command script was ran with

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scruff6119
    New Member
    • Oct 2008
    • 5

    how to get exact command script was ran with

    Example:
    [PHP]bash$ cmd.pl -m "202.2.13.3 3 24.32.2.45" -n testing -r "2 3 4 5"[/PHP]

    Options are processed by Getppts::Long. I save off @ARGV before it is processed. Unfortunately, the quotes are required because Getopts::Long in the older Perl version we use won't process multiple unquoted params per argument.

    now @ARGV contains:
    "-m, 202.2.13.33, 24.32.2.45, -n, testing, -r, 2, 3, 4, 5"

    I need to have the exact command the user entered to start cmd.pl including the quotes around the parameters for later use so @ARGV is not very helpful here.

    Does anyone have a simple trick to accomlish this? So far all I've come up with is a convoluted for loop/regex to reconstruct the string to be "correct", but the user might not have run the script correctly so I really need to stay true here.
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    Originally posted by scruff6119
    Example:
    [PHP]bash$ cmd.pl -m "202.2.13.3 3 24.32.2.45" -n testing -r "2 3 4 5"[/PHP]

    Options are processed by Getppts::Long. I save off @ARGV before it is processed. Unfortunately, the quotes are required because Getopts::Long in the older Perl version we use won't process multiple unquoted params per argument.

    now @ARGV contains:
    "-m, 202.2.13.33, 24.32.2.45, -n, testing, -r, 2, 3, 4, 5"

    I need to have the exact command the user entered to start cmd.pl including the quotes around the parameters for later use so @ARGV is not very helpful here.

    Does anyone have a simple trick to accomlish this? So far all I've come up with is a convoluted for loop/regex to reconstruct the string to be "correct", but the user might not have run the script correctly so I really need to stay true here.
    One way is:
    Code:
    $_ =~ s/^(.+\s+.+)$/"$1"/ foreach(@ARGV) ;
    But if you totally want to avoid using regex and foreach loop, you have to take care while passing arguments to script. It should be like:
    Code:
     cmd.pl -m "\"202.2.13.33 24.32.2.45\"" -n testing -r "\"2 3 4 5\""

    Comment

    • scruff6119
      New Member
      • Oct 2008
      • 5

      #3
      Thanks, that regex is simpler than mine at least. I've even thought of some more really kludgy ways of pulling this off, but in the end I might just use this one liner and damn if the user entered the command incorrectly and didn't read the error he/she was provided at runtime :)

      Comment

      Working...