using variables in perl not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using variables in perl not working
# 1  
Old 01-24-2012
using variables in perl not working

sdcprd@dotstoas110:$ echo $F2
1327332411

this works
----------
sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime (1327332411))'
012312 <<<< correct date


this doesnt
-----------
sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime ($F2))'
123169
sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime (${F2}))'
123169

how can i use variable in this perl expression ?

thanks

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-24-2012 at 12:10 PM..
# 2  
Old 01-24-2012
Variables do not expand inside single quotes in the shell.

Code:
$ VAR="1234"
$ echo '$VAR'
$VAR
$ echo "$VAR"
1234
$

So put your perl expression in double quotes.

You'll have to escape the double quotes inside your perl like \" otherwise the shell will consider them the end of the string. Other than that I don't see anything the shell will mangle.
# 3  
Old 01-24-2012
iam not sure i understand you can you please modify this command to act properly ?
Code:
var=`perl -MPOSIX -le 'print strftime ("%m%d%y",localtime ($F2))'`

thanks
Moderator's Comments:
Mod Comment Please use next time code tags for your code and data


---------- Post updated at 11:18 AM ---------- Previous update was at 11:06 AM ----------

I tried this , didnt work

sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime ("${F2}"))'
123169

---------- Post updated at 11:20 AM ---------- Previous update was at 11:18 AM ----------

also escaping the variable didnt work

sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime ("$\F2"))'
123169

Last edited by vbe; 01-24-2012 at 12:11 PM..
# 4  
Old 01-24-2012
When hitting the ' key, hit shift so it becomes " instead.

Things inside " quotes expand in the shell, things inside ' don't. That's what I demonstrated.

Your variable will never expand in there because you put it in ' quotes.
# 5  
Old 01-24-2012
$F2 is not a perl variable, It's in the parent shell, you need to export it and use $ENV{F2} to refer to it in Perl
# 6  
Old 01-24-2012
Or you could just use double quotes like I said twice...
# 7  
Old 01-24-2012
can anyone give me the syntax please ? I am new to unix scripting and struggling .

---------- Post updated at 12:17 PM ---------- Previous update was at 12:00 PM ----------

Corona I heard you say twice but iam not understanding where to put quotes can you please show me ?

---------- Post updated at 12:24 PM ---------- Previous update was at 12:17 PM ----------

SKyrynsaver: How can i evaluate the variable n use it inside perl ?

if you can point it out with my command that iam trying to use

thanks

---------- Post updated at 12:24 PM ---------- Previous update was at 12:24 PM ----------

SKyrynsaver: How can i evaluate the variable n use it inside perl ?

if you can point it out with my command that iam trying to use

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output >ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null somefilename_27.sometxt somefilename_28.sometxt somefilename_29.sometxt .. somefilename_97.sometxt But if i want... (2 Replies)
Discussion started by: haiderali
2 Replies

2. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

3. Shell Programming and Scripting

Working with lines or variables that have spaces or special characters

Example: while read line do stat -c %G $line done < somefile.txtThe problem is that inside somefile.txt lines can have any symbol allowed as file name, like (). Even with spaces, it splits the words. somefile.txt:dira/my first jump.avi dirb/surf video (1080p).mkv (2 Replies)
Discussion started by: Tribe
2 Replies

4. Shell Programming and Scripting

Variables not working

Hello, I have a (basic I guess) problem with bash scripting. In the command line, this piece of code returns nothing (a blank line), and if I'm not worng it should return 3. set VAR = "3" echo $VAR if I do this (also in command line), a zero is returned: set VAR = 3 printf "%d"... (4 Replies)
Discussion started by: jlseminara
4 Replies

5. Shell Programming and Scripting

echo two variables like the paste command is not working

Dear all, I have two files like this file1 A B C D E F file2 1,2 3,4 5,6 I want this output output_expected A B 1,2 C D 3,4 E F 5,6 (3 Replies)
Discussion started by: valente
3 Replies

6. Shell Programming and Scripting

Variables in perl

I don't fully understand variables in perl. If we have a variable defined like this "my $number = 1" then this is called a lexical variable? But if you define this at the top of a script then why isn't it a global variable because it would be available throughout the file? Sorry if this is... (1 Reply)
Discussion started by: P3rl
1 Replies

7. Shell Programming and Scripting

[Solved] Working with date (add minutes using variables)

Dear all, today I'm scratching my head with a simple (I believe) issue. Working with date is quite simple, so if I Need to add some seconds to current time, I'll use: date --date='+30 seconds' +"%Y-%m-%d %H:%M:%S"But, how to pass the value to add from a variable? I tried the following without... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

8. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

9. UNIX for Dummies Questions & Answers

Working with Script variables; seems like this should work...

The following seems quite basic but does not seem to work. Anybody know why? $ g=1 $ echo $g 1 $ echo abc$g abc1 $ abc$g=hello ksh: abc1=hello: not found $ echo $abc1 ksh: abc1: parameter not set It works when I specify the full variable name $ abc1=hello $ echo $abc1 hello ... (2 Replies)
Discussion started by: Chong Lee
2 Replies

10. AIX

Setting Variables not working

Hi all, I am trying to set up some variables in a shell script. The variables contain values of various paths needed to run a java module. The problem is the variables dont seem to be setting at all. here is what i am trying to do : JAR_HOME=/home/was5/bdcms/scheduledjobs/lib export... (1 Reply)
Discussion started by: rpandey
1 Replies
Login or Register to Ask a Question