By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,374 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,374 IT Pros & Developers. It's quick & easy.

how to get exact command script was ran with

P: 5
Example:
[PHP]bash$ cmd.pl -m "202.2.13.33 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.
Oct 24 '08 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
Example:
[PHP]bash$ cmd.pl -m "202.2.13.33 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:
Expand|Select|Wrap|Line Numbers
  1. $_ =~ s/^(.+\s+.+)$/"$1"/ foreach(@ARGV) ;
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1.  cmd.pl -m "\"202.2.13.33 24.32.2.45\"" -n testing -r "\"2 3 4 5\""
  2.  
Oct 24 '08 #2

P: 5
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 :)
Oct 24 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.