How do I pass $SHELL variable into a perl search and replace -


i have following 2 commands :

value=`grep -o logs\/.*txt\" textfile` perl -i -wpe's/" onclick="img=document\.getelementbyid\('\''img_1'\''\); img\.style\.display = \(img\.style\.display == '\''none'\'' \? '\''block'\'' : '\''none'\''\);return false"/$value/' textfile 

however, keep on getting use of uninitialized value $value in concatenation (.) or string @ -e line 1, <> line 56. error. can tell me doing wrong.

for example following:

$ value=$(uname -r) $ perl -e 'print "$value"' 

when run perl execute our code here not sending contents of shell variable $value perl. sending literal string '$value', perl try , print perl variable $value, not exist.

here 2 ways correct this.

the first wrap perl code in double quotes instead of single quotes $value becomes contents of shell variable $value instead of literal string '$value' when sent perl:

$ value=$(uname -r) $ perl -e "print '$value'" 3.13.9-generic 

however when using method if have perl variables along shell variable need 'escape' perl variables or you'll error this:

$ value=$(uname -r) $ perl -e "$example = 'test'; print \"$value $example\";" syntax error @ -e line 1, near "=" 

using \'s correct this:

$ value=$(uname -r) $ perl -e "\$example = 'test'; print \"$value \$example\";" 3.13.9-generic test 

secondarily, can avoid changing single quotes double quotes exporting "value" environment, , accessing through %env hash available inside perl program (http://perldoc.perl.org/env.html). example:

$ export value=$(uname -r) $ perl -e 'print "$env{value}\n"' 3.13.9-generic 

be mindful of environment not accidentally overwrite needed environment variable. using naming convention specific work can help:

$ export myprogramname_kernel_version=$(uname -r) $ perl -e 'print "$env{myprogramname_kernel_version}\n"' 3.13.9-generic 

printenv on linux systems show current established environment variables. use perl inspect %env.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -