I've found some documentation on using the perl -s switch to get variables from the command line, but that's not really what I want. I'd like to take a command line argument that is a string that includes a command and a variable in the Perl script to print.
#!/usr/bin/perl use strict; my $host = 'localhost'; # Use 1 of the following lines: #my $output = eval($ARGV[0]); my $output = $ARGV[0]; print "COMMAND: $output\n"; system($output);
Now call with:
script.pl "echo hello"
Output is:
{C} > script.pl "echo hello" COMMAND: echo hello hello
However, try:
{C} > test "echo $host" COMMAND: echo $host $host
I'd like "$host" that is printed to actually be "localhost" as is set in the script (my $host = 'localhost';) that is output. I had a feeling the "eval" would be needed, but that only works when the argument passed is the variable itself (ie: "$host", not "echo $host" and I need it to be the later since I'd like to pass more complex commands other than just 'echo').
In reply to Command Line Arg contains Variable and more by VinsWorldcom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |