Hello Monks!
I have script which uses GetOptions to parse options. My script allows the to use the --job option to pass everything the user wants (scripts, additional parameters, etc.). For example:
my_script.pl --option1 --option2 --job some_job.pl -option1 --option2
+--option3
My script runs the
some_job.pl -option1 --option2 --option3 part at some point. Before the GetOptions parse, I have the following code:
my $job_flag = 0;
foreach my $i (0..$#ARGV) {
​$opt{"job"} .= $ARGV[$i]." " if($job_flag);
​$job_flag = 1 if($ARGV[$i] =~ /^(\-\-|\-)job$/);
​undef $ARGV[$i] if($job_flag);
}
The code just saves everything after --job into the $opt{job} and removes it from the ARGV so GetOptions won't parse it. Now I noticed that if there is quotes inside the job part, it will remove those quotes. For example:
my_script.pl --option1 --option2 --job some_job.pl -option1 'some_job2
+.pl -x abc' -option2
Then the Dumper of ARGS is:
$VAR1 = [
+
+
'--option1',
'--option2',
'--job',
'some_job.pl',
'-option1',
'some_job2.pl -x abc',
'-option2'
];
I would expect:
$VAR1 = [
+
+
'--option1',
'--option2',
'--job',
'some_job.pl',
'-option1',
'\'some_job2.pl -x abc\'',
'-option2'
];
How can I know that there were quotes? I guess I'll need to think of another way to do this.
So there won't happen and X-Y problem, I'll explain what I'm trying to do. I want to get user's command and execute it at some point. It could have parameters and I don't want the user to insert it into file first. How can it be done?
2021-06-25 Athanasius fixed code tag.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.