473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to read inputs from file for perl script?

i am running my perl script as
"perl script.pl /root/sample/shadow .htpasswd"
now i need to pass these inputs in separate file
and read from that file.

for eg
content of file may be
/root/sample/shadow .htpasswd
/root/sample/shadow .htpasswd1
/root/sample/shadow .htpasswd2
/root/sample/shadow .htpasswd3

and my script is for comparing two files listed above.
Feb 15 '08 #1
15 2257
KevinADC
4,059 Expert 2GB
I don't understand your question
Feb 15 '08 #2
nithinpes
410 Expert 256MB
If you are trying to compare two files, you can send the path of two/more files as arguments to the script separated by space directly :
Expand|Select|Wrap|Line Numbers
  1. perl script.pl "/root/sample/shadow .htpasswd" "/root/sample/shadow .htpasswd1" "/root/sample/shadow .htpasswd2"
  2.  
Inside thescript, you can access these files through @ARGV. For ex:
Expand|Select|Wrap|Line Numbers
  1.  open(F1,"$ARGV[0]") or die "failed to open first file";
  2.  open(F2,"$ARGV[1]") or die "failed to open second file";
  3.  
  4.   while(<F1>)
  5. {
  6.   ### read through first file line by line
  7. }
  8.  
  9.  
Feb 15 '08 #3
If you are trying to compare two files, you can send the path of two/more files as arguments to the script separated by space directly :
Expand|Select|Wrap|Line Numbers
  1. perl script.pl "/root/sample/shadow .htpasswd" "/root/sample/shadow .htpasswd1" "/root/sample/shadow .htpasswd2"
  2.  
Inside thescript, you can access these files through @ARGV. For ex:
Expand|Select|Wrap|Line Numbers
  1.  open(F1,"$ARGV[0]") or die "failed to open first file";
  2.  open(F2,"$ARGV[1]") or die "failed to open second file";
  3.  
  4.   while(<F1>)
  5. {
  6.   ### read through first file line by line
  7. }
  8.  
  9.  
i need to give all the inputs in one file and read from that file.
u said to pass all arguments simultaneously, but i want to write those argument
in a file read from that.
Feb 15 '08 #4
nithinpes
410 Expert 256MB
i need to give all the inputs in one file and read from that file.
u said to pass all arguments simultaneously, but i want to write those argument
in a file read from that.
In that case, you have to run your original script from another script.
Write another script (e.g new.pl), and run the script as below:

perl new.pl paths.txt

where paths.txt is file containing filenames. Inside new.pl, you call the other script.

Expand|Select|Wrap|Line Numbers
  1.  
  2. while(<>)  ## read from file line by line(one file at a time)
  3. {
  4.   `perl script.pl "$_" ` ;  
  5. ## $_ is put between " " to include paths containg spaces in between. Not required otherwise
  6.  
There may be better ways of doing it. Hope, this is what you wanted.
Feb 15 '08 #5
In that case, you have to run your original script from another script.
Write another script (e.g new.pl), and run the script as below:

perl new.pl paths.txt

where paths.txt is file containing filenames. Inside new.pl, you call the other script.

Expand|Select|Wrap|Line Numbers
  1.  
  2. while(<>)  ## read from file line by line(one file at a time)
  3. {
  4.   `perl script.pl "$_" ` ;  
  5. ## $_ is put between " " to include paths containg spaces in between. Not required otherwise
  6.  
There may be better ways of doing it. Hope, this is what you wanted.
i can't get it clearly.
As u said paths.txt i wrote like
/root/sss/shadow /root/.htpasswd
/root/sss/shadow /root/.htpasswd1
/root/sss/shadow /root/.htpasswd2

then new.pl
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. while(<>) 
  3. {
  4. `perl script.pl "$_" ` ; 
  5. }
but i get error like
cannot open: No such file or directory in script.pl at line 2.
Feb 15 '08 #6
nithinpes
410 Expert 256MB
That may be because you are not chopping newline from the input,remove the trailing new line and try.
new.pl
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. while(<>) 
  3. {
  4.  chomp;
  5. `perl script.pl "$_" ` ; 
  6. }
  7.  
  8.  
Feb 15 '08 #7
That may be because you are not chopping newline from the input,remove the trailing new line and try.
new.pl
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. while(<>) 
  3. {
  4.  chomp;
  5. `perl script.pl "$_" ` ; 
  6. }
  7.  
  8.  
again i am getting the same problem.
Feb 15 '08 #8
nithinpes
410 Expert 256MB
again i am getting the same problem.
Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl
Feb 15 '08 #9
Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl

hi boss now i am getting the output correctly.
i forgot to remove " " in new.pl. after removed it i
got the output.
thnx for ur help.
Feb 15 '08 #10
nithinpes
410 Expert 256MB
Then, it means file is not present in that location/ it is inaccessible. The error reported is in script.pl, not new.pl
In what format are those files in?How are you trying to open the file inside script.pl?
If you post your code, it would be helpful to identify..
Feb 15 '08 #11
nithinpes
410 Expert 256MB
Oops! We both posted message at the same time. Ok then.

Happy coding !!
Feb 15 '08 #12
KevinADC
4,059 Expert 2GB
I am still not even sure what the OP is doing, but it seems rather odd to shell out to perl from a perl script:

`perl script.pl "$_" ` ;
Feb 15 '08 #13
I am still not even sure what the OP is doing, but it seems rather odd to shell out to perl from a perl script:

`perl script.pl "$_" ` ;
I got OP by removing " " in above statement. since there is no space in my
file path no need of putting " ".
Feb 16 '08 #14
KevinADC
4,059 Expert 2GB
I got OP by removing " " in above statement. since there is no space in my
file path no need of putting " ".

Thats fine, but shelling out to a perl program from within a perl program seems totally unecessary.
Feb 16 '08 #15
nithinpes
410 Expert 256MB
I got OP by removing " " in above statement. since there is no space in my
file path no need of putting " ".
Senthilkumar,
I do agree with Kevin. Shelling out to a Perl script may be unnecessary in your scenario.
You can simplify it by including the while loop inside your original script and opening one file at a time, if that meets the objective. Another way is to convert your script to a subroutine and call the sub-routine for each iteration of while loop (each file).
You would have met your objective. But this way, script would be more efficient :)
Feb 18 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
3
by: FLOTServer | last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor". On the game server is log file which contains all of the data from the players for that day (kills, deaths, etc...). I...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
0
by: Titof | last post by:
Hello, i want to create an active webpage that displays the status of users quotas on a volume of a server. I installed IIS on this server and installed Win2k server Resource Kit because the...
2
by: sangeetha | last post by:
hi everybody i want to run one perl script with three inputs inside the expect script i dont know how to automatically run the perl script with three inputs .... can u please guide me...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
1
by: pragatid | last post by:
Hi, I am new to Perl programming. Let me explain my requirement over here. I have a Perl script located in /usr/sbin/ say script A and I want to invoke this script via web after accepting some user...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
1
by: krus | last post by:
Hi All, I am relatively new to HTML and perl scripts. I am writing a HTML page which has a form. It takes two inputs. Now I want pass these two inputs from the HTML form to the perl script. I want...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.