problem with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with perl
# 1  
Old 05-29-2008
problem with perl

hi,

i have a script that coverts the file time in epoch time.but the problem is perl is not working inside the k-shell
----------------------------------------------------------------

#!/bin/ksh

echo "Enter the file"
read n

perl -e 'print ((stat("n"))[9])'
----------------------------------------------------------------------

but perl is working in the command prompt.
# 2  
Old 05-29-2008
You are not passing the value of n to Perl correctly.

Code:
perl -le 'print ((stat($ARGV[0]))[9])' "$n"

I took the liberty to add an -l (ell) option to add a newline to the printed value.

You could also use quoting to interpolate the value of n directly into the script:

Code:
perl -le 'print ((stat("'"$n"'"))[9])'

Read up on quoting to understand how that works.

Last edited by era; 05-29-2008 at 07:03 AM.. Reason: Also show how to interpolate $n into script
# 3  
Old 05-29-2008
thanks i got it now. thanks a lot dear,

can u tell me how to put the value into a variable..

i mean just like this:

value= perl -le 'print ((stat($ARGV[0]))[9])' "$n"
# 4  
Old 05-29-2008
Use backticks. These are grave accents (ASCII 96) not regular apostrophes.

Code:
value=`perl -le 'print ((stat($ARGV[0]))[9])' "$n"`

Modern shells also offer the alternative syntax value=$(command ...) which is somewhat more readable.
# 5  
Old 05-29-2008
thanks again for ur help.......one more request help me in this

-rwxrwxrwx 1 pipe pipe 369 May 29 05:05 s3
-----------------------------------------------------------------------

#!/bin/ksh

echo "Enter the file"
read n

value=`perl -le 'print ((stat($ARGV[0]))[9])' "$n"`
echo $value

perl -le 'print scalar localtime($value);'
-----------------------------------------------------------------------

o/p:

Enter the file
s3
1212055511
Wed Dec 31 18:00:00 1969
----------------------------------------------------------------------

the date of s3 is May 29 05:05 but its giving Dec 31 18:00:00 1969 ?
# 6  
Old 05-29-2008
Again, you are not passing $value to Perl. Perl doesn't know what variables exist in the shell (except via the environment mechanism %ENV) -- you have to pass it in somehow.

Code:
perl -le 'print scalar localtime(shift @ARGV)' "$value"

or

Code:
perl -le 'print scalar localtime('"$value"')'

You really do need to understand what this does. We are passing in a Perl script in three pieces. The first piece is 'print scalar localtime(' and the third part is ')' and the part in between is expanded by the shell into the value of the shell variable $value. So what Perl sees and executes in this case has no variables at all; it effectively executes the following script:

Code:
perl -le 'print scalar localtime(1212055511)'

and has no idea where the value 1212055511 comes from.
# 7  
Old 05-29-2008
many many thanks to u. actaully i m not so good in perl. mostly i use k-shell commands.

thanks a lot for ur precious help...........
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl eval problem

Hello All, I am trying to use perl eval in a complex code and below given is a pseudo code of my logic. Here $result evalutes to empty. Please help.How should I retrieve of $t where $f just hold the name of varaible i.e t $t=10; $f='$t'; $result=eval "\$$f"; print "$result\n"; (3 Replies)
Discussion started by: prasperl
3 Replies

2. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

3. Shell Programming and Scripting

queue problem perl

Hi, I have implement queue but it take last element instead of fist element #!/usr/bin/perl -w print "Enter page Access"; chomp ($item = <STDIN>); my @queue = split /\s+/, $item; print "Enter a page frame"; $frame = <STDIN>; if ( $frame >= 3 ) { $fifo = pop @queue; print... (1 Reply)
Discussion started by: guidely
1 Replies

4. Shell Programming and Scripting

Problem with perl openssh

hello, can anyone help me with this problem? im just a beginner and currently starting to program in perl, also i just installed openssh because what i really need is to ssh to a ubuntu server. so I tried my code: #!/usr/bin/perl use strict; use warnings; use Net::OpenSHH; my $ssh =... (4 Replies)
Discussion started by: samohung390
4 Replies

5. Shell Programming and Scripting

Execution problem with perl

I got the below error when using the below code...it seem that perl interpret the "'" in the middle and therefore the pipe is not finished. perl -wle ' @a=`who| perl -wlane 'print \$F;' | sort -u` ; chomp @a ; print @a; ' the error message in cygwin is:- perl: No match. | sort... (12 Replies)
Discussion started by: ahmad.diab
12 Replies

6. Programming

Problem with perl ~ tr///

I am trying to run the following script which is a file format converter. The frame variable in the input file has a file of 3,2,1 which needs to be 0,1,2 respectively i.e. 3 =0 etc. I have included the tr/// function into the script to do this, however it does not seem to be working input its... (2 Replies)
Discussion started by: fordie
2 Replies

7. Shell Programming and Scripting

Perl- matrix problem

A C G T - A 5 -4 -4 -4 -5 C -4 5 -4 -4 -5 G -4 -4 5 -4 -5 T -4 -4 -4 5 -5 - -5 -5 -5 -5 0 So lets say I have a matrix which looks something like (above). Its basically a scoring matrix. the numbers are... (2 Replies)
Discussion started by: aj05
2 Replies

8. Shell Programming and Scripting

grep problem in perl

Hi, I'm writing one perl script to select only a number part from a line. These lines looks as: raj born on 20080201 at delhi result for the equation is (886005) peter get married on 20031230 Maria born on 20080201 at paris my output file should only contains the numbers, by removing... (1 Reply)
Discussion started by: Lokesha
1 Replies

9. Shell Programming and Scripting

Problem with if statement in perl

if (($fields eq $hwp) && ($fields eq 'Y')) { $fields = "INTEGRAL"; } elsif ($fields eq $hwp) { $fields = "INTEGRAL"; } elsif ($fields ne $hwp) { $fields = "SEPARATE"; } print "$fields $fields $fields\n"; Output: The problem here is that the first... (2 Replies)
Discussion started by: kamitsin
2 Replies

10. Shell Programming and Scripting

Perl problem

I have been recently given a PERL script to develop, but the main problem is that the perl version that I have to use is old, also I cant download modules from CPAN. Perl version 5.0005 I didnt realise this untill I had the script ready to be tested, so there are a few modules that I have... (6 Replies)
Discussion started by: meevagh
6 Replies
Login or Register to Ask a Question