0

can you please tell me how to pass the cshell variable to a perl one liner? i expect the script to print out AAA but it ends up giving me an error: Substitution replacement not terminated at -e line 1.

#!/usr/bin/tcsh -fb

set myvar = "AAA"
perl -e "print $myvar"

the more complex case for my script.

#!/usr/bin/tcsh -fb

set myvar = "AAA"
perl -ne 'if (/$myvar/) {s/m1/m2/g}' fileA
user466130
  • 357
  • 7
  • 19
  • actually this is just a simplified version of my script, i would like to process an input file said fileA....my script is something like: perl -ne 'if (/$myvar/) {s/m1/m2/g} fileA – user466130 Aug 29 '13 at 04:13
  • possible duplicate of [How can I pass on my Bash loop variable to the Perl interpreter?](http://stackoverflow.com/questions/8974493/how-can-i-pass-on-my-bash-loop-variable-to-the-perl-interpreter) – Zaid Aug 29 '13 at 06:11

3 Answers3

1

Environment variables from the host system are available in Perl's %ENV variable. But to mark a variable as inheritable you have to use csh's setenv builtin instead of set.

% setenv myvar AAA
% perl -e 'print $ENV{myvar}'
AAA

(Note the single quotes in the argument to perl -e)

mob
  • 117,087
  • 18
  • 149
  • 283
1

Your perl statement is print 'AAA', so you need to pass the single quote in.

set myvar = "AAA"
perl -e "print '$myvar'"

In the other case, you need to use double-quotes to let Perl get the value of your variable.

set myvar = "AAA"
perl -ne "if (/$myvar/) {s/m1/m2/g}" fileA
Ade YU
  • 2,292
  • 3
  • 18
  • 28
0

You can pass myvar as command line argument to your perl program.

Bill
  • 5,263
  • 6
  • 35
  • 50