Where can I find the list of reserved variable names for mib2c "language"? I mean the possible variables that are not described here, like ${name}, which evaluates to the OID name that is passed as the argument to mib2c.c.
Are there any other variables like that?
Is there one that carries the name of the .conf file that was passed to the mib2c.
Looking at local/mib2c from net-snmp-5.7.3, the pre-populated variables are populated by the following code:
$outputName = $mibnode->{'label'} if (!defined($outputName));
$outputName =~ s/-/_/g;
$vars{'name'} = $outputName;
$vars{'oid'} = $oid;
$vars{'example_start'} = " /*\n" .
" ***************************************************\n" .
" *** START EXAMPLE CODE ***\n" .
" ***---------------------------------------------***/";
$vars{'example_end'} = " /*\n" .
" ***---------------------------------------------***\n" .
" *** END EXAMPLE CODE ***\n" .
" ***************************************************/";
So, you end up with the following pre-populated variables:
$name is the "output prefix" specified using the -f option (or $mibnode->{'label'}, whatever that is, if the -f option wasn't used), with dashes substituted with underscores.
$oid is the value of mib2c's argument (called "mibNode" in the usage help).
$example_start and $example_end are hard-coded strings.
That's it.
To create $config with the value of the -c argument (or mib2c.conf if the -c option wasn't used), you could alter mib2c to add the following to the assignments shown above:
$vars{config} = $configfile;
Alternatively, I believe the following will also create $config, but the value passed to the -c option will be prepended with a directory name:
#perleval $vars{config} = $configfile; 0#
You could try to obtain the original value with the following (which assumes the original value didn't contain a /);
#perleval $vars{config} = $configfile =~ m{([^/]+)\z}s ? $1 : undef; 0#
Completely untested. I don't know anything about SNMP or mib2c.
Related
I want add the variable $hostname in the variable $hostame_table appended with _table.
My code:
use Sys::Hostname ();
my $hostname = Sys::Hostname::hostname();
my $hostname_table = "$hostname"+"_table";
print "$hostname_table";
I would like the result to be computername_table.
What you're looking for here is called "string concatenation". Perl uses a dot (.) as the concatenation operator, not the + which you've tried to use.
$hostname_table = $hostname . '_table';
print $hostname_table;
Update: Another common approach would be to just "interpolate" the values in a double-quoted string.
$hostname_table = "$hostname_table"; # This doesn't work.
Unfortunately, this simple approach doesn't work here as "_table" is valid as part of a variable name and, therefore, Perl doesn't know where your variable name ends. You can get around that by using a slightly more complex syntax which wraps the variable name in { ... }.
$hostname_table = "${hostname}_table";
I have a question regarding parsing command line arguments and the use of the shift command in Perl.
I wanted to use this line to launch my Perl script
/home/scripts/test.pl -a --test1 -b /path/to/file/file.txt
So I want to parse the command line arguments. This is part of my script where I do that
if ($arg eq "-a") {
$main::john = shift(#arguments);
} elsif ($arg eq "-b") {
$main::doe = shift(#arguments);
}
I want to use then these arguments in a $command variable that will be executed afterwards
my $var1=$john;
my $var2=$doe;
my $command = "/path/to/tool/tool --in $line --out $outputdir $var1 $var2";
&execute($command);
Now here are two problems that I encounter:
It should not be obligatory to specify -a & -b at the command line. But what happens now is that when I don't specify -a, I get the message that I'm using an uninitialized value at the line where the variable is defined
Second problem: $var2 will now equal $doe so it will be in this case /path/to/file/file.txt. However I want $var2 to be equal to --text /path/to/file/file.txt. Where should I specify this --text. It cannot be standardly in the $command, because then it will give a problem when I don't specify -b. Should I do it when I define $doe, but how then?
You should build your command string according to the contents of the variables
Like this
my $var1 = $john;
my $var2 = $doe;
my $command = "/path/to/tool/tool --in $line --out $outputdir";
$command .= " $var1" if defined $var1;
$command .= " --text $var2" if defined $var2;
execute($command);
Also
Don't use ampersands & when you are calling Perl subroutine. That hasn't been good practice for eighteen years now
Don't use package variables like $main:xxx. Lexical variables (declared with my) are almost all that is necessary
As Alnitak says in the comment you should really be using the Getopt::Long module to avoid introducing errors into your command-line parsing
GetOpt::Long might be an option: http://search.cpan.org/~jv/Getopt-Long-2.48/lib/Getopt/Long.pm
Regarding your sample:
You didn't say what should happen if -a or -b are missing, but defaults may solve your problem:
# Use 'join' as default if $var1 is not set
my $var1 = $john // 'john';
# Use an empty value as default if $var2 is not set
my $var2 = $doe // '';
Regarding the --text prefix:
Do you want to set it always?
my $command = "/path/to/tool/tool --in $line --out $outputdir $var1 --text $var2";
Or do you want to set it if -b = $var2 has been set?
# Prefix
my $var2 = "--text $john";
# Prefix with default
my $var2 = defined $john ? "--text $john" : '';
# Same, but long format
my $var2 = ''; # Set default
if ($john) {
$var2 = "--text $john";
}
I have a string "/Name Pa$Name#my"
$myname = GetContent("Name")
GetContent is only gets the key and gives the value for that Key. I cannot modify the GetContent function.
If i execute above, as "Pa$Name#my" contains the '$' char, $myname gives me value as "Pa#my" and ignores "$Name" content.
What can i do to get the $myname = Pa$Name#my ? Can i append some special characters while assigning the $myname variable.
As far as I understand your trouble comes from the fact that the var $name does not exist. You can use single qotes if you don't want Powershell to look for vars :
$a = 'Pa$Name#my'
I generally use the following format when I populate a script with variables:
$string = ("Hi my name is {0} and I live in {1}, {2}." -f $username,$usercity,$userstate)
However this doesn't seem to work with an array, or I might be getting the syntax completely wrong:
$Arguments = #("/Settings:{0}", "/Tests:{1}", "/output:{2}" -f $TestSetting,$TestList,$Output)
When I output the results of that association, it all comes out as one string (with the substitution correct). If I look at the Count, it is 1. What am I missing?
Each value in the array needs to be it's own expression with it's own operator:
$Arguments = #(("/Settings:{0}" -f $TestSetting), ("/Tests:{0}" -f $TestList), ("/output:{0}" -f $Output))
Suppose I have files named like GATES, Bill.jpg and I want to rename them all to Bill Gates.jpg. I can capture the two words
rename 's/^(.*?), (.*?)\./$2 $1\./g' *
To change a case there are some Perl's functions:
$lower = lc("aBcDe"); # $lower is assigned "abcde"
$upper = uc("aBcDe"); # $upper is assigned "ABCDE"
$lower = lcfirst("HELLO"); # $lower is assigned "hELLO"
$upper = ucfirst("hello"); # $upper is assigned "Hello"
I tried to make use of them:
rename 's/^(.*?), (.*?)\./$2 ucfirst($1)\./g' *
But it doesn't work.
You need to add the "e" (eval) flag to the end of the regular expression, otherwise the function won't be executed. This means that the entire second part of the s/// expression has to be a valid Perl expression (instead of a valid string):
rename 's/^(.*?), (.*?)\./"$2 " . ucfirst(lc($1)) . "."/ge' *
(also note the extra space inside the string with $2)
More information on this flag can be found in the perlre documentation.
Should be:
rename 's/^(.*?), (.*?)\./$2 \u\L$1./g' *
Although that doesn’t always work perfectly on Unicode. For those few corner cases it misses, you would want something more like
rename 's/^(\w)(\w*),\s+(\w+)\./$3 \u$1\L$2./g' *
Here’s where you can get a somewhat updated version of the regular rename program.