#!/bin/bash
export IPV6=$1
expanded_ipv6_addr=`perl -e 'require "/usr/bin/ipv6_helper.pm"; $expand_ipv6=expand_ipv6_addr($ENV{IPV6});print $expand_ipv6'`
I don't want to export the $IPV6 variable, so I am looking any other way to do this.
Grab the value from #ARGV:
expanded_ipv6_addr=$(
perl -e '
require "/usr/bin/ipv6_helper.pm";
print expand_ipv6_addr(shift)
' "$IPV6"
)
Instead of exporting $1 into an environment variable you could use it again later and escape the perl code.
The following worked for me with a stubbed out version of /usr/bin/ipv6_helper.pm
#!/bin/bash
IPV6=$1
expanded_ipv6_addri=`perl -e "
require \"/usr/bin/ipv6_helper.pm\";
\\$expand_ipv6 = expand_ipv6_addr($IPV6);
print \\$expand_ipv6
"`
Related
I am trying to execute the following gpg command from within Perl:
`gpg --yes -e -r me#mydomain.com "$backupPath\\$backupname"`;
However I get the following error:
Global symbol "#mydomain" requires explicit package name (did you forget to declare "my #mydomain"?)
Obviously I need to escape the '#' symbol but don't know how. How do I execute this command in Perl?
When you do:
`gpg --yes -e -r me#mydomain.com "$backupPath\\$backupname"`;
perl sees the #mydomain part and assumes you want to interpolate the #mydomain array right into the string.
But since there was no #domain array declared, it gives you the error.
The fix is simple: To tell perl that you want to treat #mydomain as a string and not as an array, simply put a backslash (\) before the #, like this:
`gpg --yes -e -r me\#mydomain.com "$backupPath\\$backupname"`;
Backticks run process in sub shell (slower, consumes more resources) and have some issues which you should investigate.
Following code demonstrates other approach which does not spawn sub shell.
use strict;
use warnings;
use feature 'say';
my $backupPath = '/some/path/dir';
my $backupname = 'backup_name';
my $command = "gpg --yes -e -r me\#mydomain.com $backupPath/$backupname";
my #to_run = split ' ', $command;
system(#to_run);
Handling backticks in Perl
Problem with backticks in multi-threaded Perl script on Windows
I have a file "temp.txt" containing
var1=hello
var2=ello
var3=mello
....and a long list
in unix shell we can simply use . temp.txt is use all the variables in the file..
do we have a similar function in perl or any work around..
I tried a workaround
$ cat checkDOTfuctionOFunix.ksh
#!/bin/ksh
. /export/home/temp.txt
#export
/export/home/checkDOTfuncPRINTSinPERL.pl
$ cat /export/home/checkDOTfuncPRINTSinPERL.pl
#!/usr/local/bin/perl
print "var1=$ENV{var1} \n\n var2=$ENV{var2} \n\n";
but this wont work unless I export each value which can be done with simple sed -e 's/^/export/ but I prefer not to do this. please help :)
set -a
. temp.txt
set +a
./checkDOTfuncPRINTSinPERL.pl
with set -a as explained here, all variable assignments promote the variable to an environment variable.
Here some doc to shell set options.
This is now possible in Perl with the Env::Modify module.
use Env::Modify 'source';
source("temp.txt"); # like saying . temp.txt in the shell
.. env settings in temp.txt are now available to Perl ...
I have a perl module with data definitions (hashes, arrays, etc.), is there any way I can access that data from inside a bash script? This isn't working for me...
#!/bin/bash
perl -e 'use Data'
tests=`perl -e "#tests"; `
echo "Perl tests = ${tests}" # prints "Perl tests = "
The module looks something like this:
our #EXPORT_OK = qw( #tests );
our #tests = qw( 1 2 3 4 5 );
If you have package variable #tests inside Data module,
perl -MData -e 'print "$_\n" for #Data::tests'
For perl 5.10 and above,
perl -MData -E 'say for #Data::tests'
You can use a module from the command line with -M
perl -MData -e'print map {"$_\n"} #tests;'
In the code you give, you run one interpreter that loads Data. It exits. You then run a second interpreter, which prints #tests. As that's the only action the second interpreter has performed, it's uninitialized, and prints nothing.
To filter non-consecutive lines of file, the below one-liner working fine:
cat filename | perl -ane 'print unless $a{$_}++'
However, when i tried to make it as an alias and do, its not working as expected
alias uniqlines " cat \!* | perl -ane 'print unless \$a{\$_}++' "
erroring out as below
a: Undefined variable.
Using tcsh shell for SunOS operating system
In bash this syntax works:
alias uniqlines="perl -ane 'print unless \$a{\$_}++' "
Here is a way that seems to work even in tcsh:
alias uniqlines 'perl -ane '"'"'print unless $a{$_}++'"'"' '
You want to use a function instead:
uniqlines(){ cat "$#" | perl -ane 'print unless $a{$_}++'; }
But this is pretty much just a fancy way of saying:
alias uniqlines=uniq
I want to wrap a Perl one-liner in a batch file. For a (trivial) example, in a Unix shell, I could quote up a command like this:
perl -e 'print localtime() . "\n"'
But DOS chokes on that with this helpful error message:
Can't find string terminator "'" anywhere before EOF at -e line 1.
What's the best way to do this within a .bat file?
For Perl stuff on Windows, I try to use the generalized quoting as much as possible so I don't get leaning toothpick syndrome. I save the quotes for the stuff that DOS needs:
perl -e "print scalar localtime() . qq(\n)"
If you just need a newline at the end of the print, you can let the -l switch do that for you:
perl -le "print scalar localtime()"
For other cool things you can do with switches, see the perlrun documentation.
In Windows' "DOS prompt" (cmd.exe) you need to use double quotes not single quotes. For inside the quoted Perl code, Perl gives you a lot of options. Three are:
perl -e "print localtime() . qq(\n)"
perl -e "print localtime() . $/"
perl -le "print ''.localtime()"
If you have Perl 5.10 or newer:
perl -E "say scalar localtime()"
Thanks to J.F. Sebastian's comment.
For general batch files under Windows NT+, the ^ character escapes lots of things (<>|&), but for quotes, doubling them works wonders:
C:\>perl -e "print localtime() . ""\n"""
Thu Oct 2 09:17:32 2008
First, any answer you get to this is command-specific, because the DOS shell doesn't parse the command-line like a uniq one does; it passes the entire unparsed string to the command, which does any splitting. That said, if using /subsystem:console the C runtime provides splitting before calling main(), and most commands use this.
If an application is using this splitting, the way you type a literal double-quote is by doubling it. So you'd do
perl -e "print localtime() . ""\n"""
In DOS, you use the "" around your Perl command. The DOS shell doesn't do single quotes like the normal Unix shell:
perl -e "print localtime();"