Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Trying to convert shell script to Perl

by DS (Acolyte)
on Aug 05, 2002 at 19:54 UTC ( #187798=perlquestion: print w/replies, xml ) Need Help??

DS has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks , I am converting an old shell script to perl , I came through this for loop but don't know how to exactlly do it in perl ..
for DIR in `ls -d [a-k]* lts mon [n-z]*` do if [ -d $DIR/bld ]; then cd $DIR/bld cbsfpfg.exe `echo "cbs"$DIR$RELEASE $DIR` cd ../.. fi done
not sure how to change the for loop.don't seem to work when I try :
my $DIR; for $DIR in `ls -d [a-k]* lts mon [n-z]*` do if (-d $DIR/bld){ system (qq(cd $DIR/bld)); system(qq(cbsfpfg.exe `echo "cbs"$DIR$RELEASE $DIR`)); system (qq(cd ../..)); } done

edited: Mon Aug 5 22:14:09 2002 by jeffa - title change (was: shell problem)

Replies are listed 'Best First'.
Re: Trying to convert shell script to Perl
by DamnDirtyApe (Curate) on Aug 05, 2002 at 22:33 UTC

    For a more Perl-ish version, try something like this...

    use Cwd ; my $curr_dir = cwd ; opendir DIR , $curr_dir ; for my $DIR ( grep { /^[a-k]/ || /^lts$/ || /^mon$/ || /^[n-z]/ } readdir( DIR ) ) { if ( -d "$DIR/bld" ) { chdir "$DIR/bld" ; system 'cbsfpfg.exe', ( 'cbs' . $DIR . $RELEASE ), $DIR ; chdir $curr_dir ; } } closedir DIR ;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: shell problem
by I0 (Priest) on Aug 05, 2002 at 20:31 UTC
    for $DIR (split/\s+/,`ls -d [a-k]* lts mon [n-z]*`){ if (-d "$DIR/bld"){ system (qq(cd $DIR/bld;cbsfpfg.exe `echo "cbs"$DIR$RELEASE $DI +R`)); } }
    Update: added split to better emulate shell script
      I don't think this approach will work any better than what the seeker posted. The problem is that the values being assigned to "$DIR" all have the line-termination character(s) attached (/[\r\n]+/), whereas this was not the case for the original shell script. You either have to "chomp $DIR" or else use the "opendir()" and "readdir()" calls, as demonstrated by DDA below.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://187798]
Approved by Rex(Wrecks)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2021-01-22 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?