Visit Our UNIX and Linux User Community


Pass csh variable to Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass csh variable to Perl
# 1  
Old 10-18-2007
Pass csh variable to Perl

Hi All,

I am trying to put the csh variable into a perl.
In the below case, i am trying to put the csh variable "var" into my perl code. I tried to use '"$var"' but i don;t think it works.

Can anybody help me pls?

Code:
#!/bin/csh

set var = `echo "xxx"`

perl myperlcode.pl file

where the perl code is

Code:
#!/usr/local/bin/perl
$[ = 1;                 # set array base to 1
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

line: while (<>) {
    chomp;      # strip record separator
    @Fld = split(' ', $_, 9999);
    if ($Fld[1] eq '**' && $Fld[8] eq '2007' && $Fld[2] =~ /'"$var"'/) {
        print $_;
            }

Input File:

ddddd
ppp
** xxx qqqqq qqq qq qq qq 2007
jjjj
kkkk
lll
ppp



Expected Output:
** xxx qqqqq qqq qq qq qq 2007
# 2  
Old 10-18-2007
Unless you are passing the value as an argument you will most probably have to export it.

Also, in perl try some form of getenv().
# 3  
Old 10-18-2007
Hi Porter,

Can you explain what is meant by passing by arguement? Any examples?

On the export part , any examples?
# 4  
Old 10-18-2007
Quote:
Originally Posted by Raynon
Can you explain what is meant by passing by arguement? Any examples?
just like you did with "file".

Quote:
Originally Posted by Raynon
On the export part , any examples?
apparently "setenv" does the trick.
# 5  
Old 10-18-2007
On the Perl side that is

$ENV{'var'}

Of course you have to export it from the shell for Perl to see it.
# 6  
Old 10-18-2007
Hi cbkihong,

Do you mean the below? I encounter some error though.
I do not know how to export. Can you give me some guidance or show me an example ?

Code:
#!/usr/local/bin/perl
$[ = 1;                 # set array base to 1
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

$ENV('var')

line: while (<>) {
    chomp;      # strip record separator
    @Fld = split(' ', $_, 9999);
    if ($Fld[1] eq '**' && $Fld[8] eq '2007' && $Fld[2] =~ /'"$var"'/) {
        print $_;
            }
}

# 7  
Old 10-19-2007
Quote:
Originally Posted by Raynon
Code:
#!/usr/local/bin/perl
$[ = 1;                 # set array base to 1
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

$ENV('var')

It's curly braces {}, not parentheses. Please retry, and if there is any errors, please quote them if you post again.

Previous Thread | Next Thread
Test Your Knowledge in Computers #990
Difficulty: Medium
UNIX System V is one of the first commercial versions of the Unix operating system and was originally developed by AT&T and first released in 1981.
True or False?

10 More Discussions You Might Find Interesting

1. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

2. Shell Programming and Scripting

Perl Csh - setenv ENV change environment variable

I have 3 programs, 1 in perl, 2 in csh: call them perl1, csh1 and run.ol I need perl1 to set csh1 variable NOLOG_qsub = "" I need perl1 to run, run.ol run.ol takes the executable and input and outputs to output run.ol#!/bin/csh -f # run.ol executable input output perl1 should... (1 Reply)
Discussion started by: austinj
1 Replies

3. Shell Programming and Scripting

pass parameters from perl to csh scripts

I use csh a lot but I don't really write csh scripts. Now I have a need to implement a security check (written in perl; verify an user input security code) into a csh script. Here is the senario: #csh 1. call the perl script 2. if the perl script returns 'true', pass on; if the perl... (1 Reply)
Discussion started by: Julian16
1 Replies

4. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

5. Shell Programming and Scripting

How to pass enviroment variable from csh to Informix sql script

Hello, I have a csh script that creates an environment variable. I want to pass the environment variable(CURR_TABLE_DATE) to an Informix sql script. Here is the csh: #!/bin/csh -f setenv INFORMIXSERVER market3_tcp setenv CURR_TABLE_DATE 20090714 set DATABASE = gm_cdr set SQL_DIR =... (0 Replies)
Discussion started by: jwoj
0 Replies

6. Shell Programming and Scripting

How to do a formatted substittion in perl with a csh variable?

Hmm quite difficult for me to express my problem in one sentence.Introduction {i am a newbee to csh perl, awk and stuff, and by surfing these forums i managed to build quite a big code using all three of them in a script. Have to admitt, it was not a good idea, and its the perfect example of how... (3 Replies)
Discussion started by: Radamez
3 Replies

7. Shell Programming and Scripting

Pass perl variable to sed

Hello, I'd like to pass a variable to a sed command in a perl script. The script is like this : #!/usr/bin/perl -w $newline="new"; system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt); But the interpretor wouldn't recognize $newline, it inserts a "\n" instead. I've also... (4 Replies)
Discussion started by: hi_ryo
4 Replies

8. Shell Programming and Scripting

[csh] How to capture output from a command and pass it on to a variable?

Hi there! I'm trying to write a script that will capture output from a command and assign it to a variable. Let's say, for example, I'd like to catch from inside the script whatever the following command outputs: ls *.aaa and put it into a variable "listoffiles". What I tried was: set... (3 Replies)
Discussion started by: machinogodzilla
3 Replies

9. Shell Programming and Scripting

how can i pass parameter with spaces to csh script

Hello all i need to pass to my shell script parameter that looks like "2 3 3" inside the script i need to use this string that looks like this "2 3 3" but when i try to print the script im getting syntax error , this is my script : set s = $1 echo $s (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

How to pass CSH variables up to the parent?

Hi .. I have a dynamic script called from a programming language called Powerhouse (4GL). The module, called QUIZ, allows the user to call shell commands from within it... i.e. !rm -f mipss156t2cmd1.bat mipss156t2tmp1.txt !printf '#!/bin/csh\n' > mipss156t2cmd1.bat !printf 'setenv... (0 Replies)
Discussion started by: ElCaito
0 Replies

Featured Tech Videos