LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2012, 09:15 AM   #1
betsy1181
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Rep: Reputation: Disabled
Turn Perl FOR loop into Korn shell script


I have a FOR loop in PERL that I wrote because my Korn shell scripting was not great. I would rather use a Korn shell in the long run, but I need help converting this PERL code to Unix. Can someone please help me?
#!/usr/bin/perl
my $mydir="/dept/ftp/incoming";
opendir(MD, $mydir) || die("Cannot open directory");

my @fileNames= readdir(MD);
closedir(MD);

foreach my $f (@fileNames){
#next unless ($f=~/\.zip$/);
next unless ($f=~/^srw\_(.*)\.zip$/);
#print "$f\n";
(my $name, my $ext) = split(/\./,$f);
#print "$name\n";

my $plainName = $name;
$plainName =~ s/^srw\_//;
print "$plainName\n";

# make directory by name
$dir="/fore_perm/data/$plainName";
$dir2="/fore_perm";
`rm -R $dir`;
`mkdir $dir`;
# unzip to directory
`unzip $mydir/$name -d $dir`;
`dos2unix $dir/*.csv`;

my $file="$dir2/name.txt";
print "$file\n";
open(FILE,">$file")||die("Cannot open file$!");
print FILE "$plainName\n";
close(FILE);
`dos2unix $dir2/name.txt`;
`cd $dir`;
`sas $dir2/srw0002p.sas`;
`mail -s "log attached." betsy@mail.com < $dir/srw0002p.log`;
}
 
Old 03-28-2012, 02:55 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
maybe you can try:
Code:
for f in `ls -1a /dept/ftp/incoming`
...

I hope this helps
 
Old 03-28-2012, 03:17 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Firstly, please use [code][/code] tags when showing code.

Secondly, why not just state what it is you are trying to do?

General rule of thumb is don't fix what is not broken ... ie. if the Perl script is working then there is no real reason
to change it.
 
Old 03-28-2012, 07:17 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Also, Perl runs much faster that shell.
There are some cmds there that have Perl equivalents, that would enhance the performance and be more robust to use/check.

http://perldoc.perl.org/functions/rmdir.html
http://perldoc.perl.org/functions/mkdir.html
http://perldoc.perl.org/functions/chdir.html
Note that calling cd as an external call only changes the pwd in the subshell created
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
WHILE loop in Korn Shell betsy1181 Linux - Newbie 5 03-04-2012 06:32 PM
Korn shell script mmahulo Linux - Newbie 9 12-17-2008 10:19 AM
korn shell loop jonlake Programming 2 03-28-2008 01:10 PM
script in korn shell disruptive Programming 15 02-22-2007 11:18 AM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 01:47 PM


All times are GMT -5. The time now is 01:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration