Perl: Filename expansion?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Filename expansion?
# 1  
Old 10-07-2003
Perl: Filename expansion?

Hi,

I do not know if my subject is right, but I at least have your attention Smilie
Recently I posted a perl question related to this.

Now when someone starts my program using an asterics:

./dirinfo.pl /orapkg/ora0*/oradata/

It can contain multiple directories. The following command shows me those directories : "ls -ad /orapkg/ora0*/oradata "

But how can I import this array? The following does not work :

$directory = @ARGV[0];
if ( $directory =~ /.*\\*.*/ ) { # This works
@array=`ls -ad $directory`; # This does not work !
@arra1=`ls -lad $directory | awk '{print $9}` # Also does not work
@arra2=`eval ls -lad $directory | awk '{print $9}`# Also does not work
}

Any help will be much apreciated !!


Regs David
# 2  
Old 10-07-2003
I suppose the filenames are expanded by the shell if filenames contain an asterisk from the command line. So for

./dirinfo.pl /home/cbkihong/*

the globbed entry '/home/cbkihong/*' is expanded to the files inside that dir, and get stuffed to the corresponding @ARGV entries. So all you need to do is to read the @ARGV array. That's all.

I don't really very understand your problem, and what you mean by those `` lines do not work. Theoretically they should work, and the lines generated by the shell command are split into individual elements in the @array. If you would like to execute the shell command using `` instead of reading from the command line, then maybe you should give more info as to what you mean by "does not work".

If you would simply like to retrieve file entries I would suggest you to do it natively in Perl, using the opendir/readdir/closedir functions instead of using shell invocation of external programs like `ls` inline in Perl programs. I generally consider this more reliable in practice and I try to refrain from shell-scripting style Perl programming unless a native Perl solution is difficult to implement. Possibly because I don't know much shell scripting myself. But this also avoids the restriction of the limit in the length of the command line I heard of if the number of characters expanded by the shell exceeds the shell size limit.
# 3  
Old 10-07-2003
Re: Perl: Filename expansion?

Quote:
Originally posted by davidg
Hi,

I do not know if my subject is right, but I at least have your attention Smilie
Recently I posted a perl question related to this.

Now when someone starts my program using an asterics:

./dirinfo.pl /orapkg/ora0*/oradata/

It can contain multiple directories. The following command shows me those directories : "ls -ad /orapkg/ora0*/oradata "

But how can I import this array? The following does not work :

$directory = @ARGV[0];
if ( $directory =~ /.*\\*.*/ ) { # This works
@array=`ls -ad $directory`; # This does not work !
@arra1=`ls -lad $directory | awk '{print $9}` # Also does not work
@arra2=`eval ls -lad $directory | awk '{print $9}`# Also does not work
}

Any help will be much apreciated !!

Regs David
When your perl script is invoked from the shell with wildcards such as the "*", the SHELL expands the arguments. Suppose your current directory only contains 3 directories named "mydir", "hisdir", "newdir". If your program is invoked as follows:

./dirinfo *dir

then the shell expands the *dir so that your program is REALLY invoked like:

./dirinfo hisdir mydir newdir

So, @ARGV will contain ("hisdir", "mydir", "newdir"). Therefore, you simply need to assign @array to @ARGV. Have I misunderstood you ?
# 4  
Old 10-07-2003
Re: Re: Perl: Filename expansion?

Quote:
[i]Originally posted by k
[B]Therefore, you simply need to assign @array to @ARGV
I think you mean to assign @ARGV to @array, not the other way round otherwise you destroyed the things in @ARGV.

By the way, that's just pretty a rewrite of what I wrote just now. Smilie
# 5  
Old 10-07-2003
Re: Re: Re: Perl: Filename expansion?

Quote:
Originally posted by cbkihong
I think you mean to assign @ARGV to @array, not the other way round otherwise you destroyed the things in @ARGV.

By the way, that's just pretty a rewrite of what I wrote just now. Smilie
yeah, my mistake .... I meant assign @ARGV to @array
clobbering the @ARGV would NOT be good Smilie
# 6  
Old 10-08-2003
Thank you all for your help. Indeed @array=@ARGV; should have been my solution. Unfortunetly I was expecting ARGV[0] to contain a * and than start working on it.
Thanks a lot for your clear answers, all of you !!

Regs David


if (@ARGV) {
@dirm = @ARGV;
foreach $dirm(@dirm) {
$dirm =~ s/\/$//g;
push(@directory, $dirm);
}
}
else {
usage();
}
# 7  
Old 01-14-2008
5 years old and this post is still helpful, thank you unix.com.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies

2. Shell Programming and Scripting

Adding timestap to filename using perl

Hello, I am trying to create a file in windows and i want the filename to have timestamp as well but something is wrong and i can not understand waht. The code that i use is the following ($cwkday,$cmonth,$cday,$ctime,$cyear) = split(/\s+/, localtime); $current_date =... (5 Replies)
Discussion started by: chriss_58
5 Replies

3. Shell Programming and Scripting

Dealing with filename spaces in Perl

The following command to replace text in place in multiple files in a directory is tripping up on filename spaces (Windows environment). I really don't know Perl. find '\\server\directory' | xargs perl -pi -e 's/textA/textB/g'Mike (2 Replies)
Discussion started by: Michael Stora
2 Replies

4. Shell Programming and Scripting

perl script to split the filename

In PERL script I have few files named theme1.htm,theme2.htm,theme3.htm and so on. now I need to write perl code to split the the filename and store only that particular digit. Example -------------- filename is theme1.htm output should be 1 another example ---------------... (5 Replies)
Discussion started by: giridhar276
5 Replies

5. Shell Programming and Scripting

how to append current date to filename.tgz in perl

i would like to know how to append current date in a filename with .tgz extension. #!/usr/bin/perl my $date = `date + %Y%m%d`; system("sudo mv /tmp/nyucs01_config_backup.tgz /misc/nyucs01_config_backup_$date.tgz"); im getting this error message: sh: line 1: .tgz: command not found (7 Replies)
Discussion started by: linuxgeek
7 Replies

6. Shell Programming and Scripting

[PERL] Cannot stat or move filename - £££F3AERO££.txt

Scenario: Users drop files into a directory which is regularly polled by my PERL process. On detecting a file my process will move it from the poll dir to a working directory. A user created a file with a £ symbol in the filename and my process now fails. e.g £££F3AERO££.txt ... (1 Reply)
Discussion started by: thefal9
1 Replies

7. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

8. Shell Programming and Scripting

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi , could anyone help me out with this problem. sample.txt has this content : u001- this is used for project1 || u002- this is used for p2|| not to be printed u003- this is used for project3 || u004- this is used for p4 || u005- this is used for project5 || u006- this is used for p6... (9 Replies)
Discussion started by: Balaji PK
9 Replies

9. Shell Programming and Scripting

directory -l filename perl

Can anybody let me know what following commands will do in perl 1.my $result = `/main/home/bin/iwex -l '$File1'`; 2.my $setcmd = "/main/home/bin/iwex -s \"$File2\" \"$File3\""; where $File1 $File2 $File3 are regular files. Please suggest something. Ur welcome (4 Replies)
Discussion started by: millan
4 Replies

10. Shell Programming and Scripting

Open filename with special characters in Perl

Hi All, I am facing a weird problem. I have got a directory structure copied from windows to Linux. Some of the folders are named like /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_1998.zip /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_2000.CSV /gfs/data/Dow Jones... (6 Replies)
Discussion started by: mitrashatru
6 Replies
Login or Register to Ask a Question