Shell Command execution through PERL

Hi Guys,

I wish to execute some shell commands through PERL.

Here is what I desire to do
1) I wish to find list of directories in current working location
2) Then go in each directory and execute few commands like
a) rm -rf *.log (Shell command)
b) coreBuilder -f cb_tests.tcl (Some program Execution)
3) cd .. (come out of that dir)

Here is code writen by me

#!/usr/bin/perl  

###         system( "/usr/bin/cd", "8000294119" );

@dir_list = ` ls -d */. `;

foreach $dir_name (@dir_list) {
    print "$dir_name";
    system("cd $dir_name");
    system("\rm -rf *.log");
    system("coreBuilder -shell -f cb_tests.tcl >& cb_tests.log");
    system("coreAssembler -shell -f ca_tests.tcl >& ca_tests.log");
    system("cd ../");
}

But it does not work. It does not go in directory, does not perform any job in the directory.

Thanks for your timely response.
Hardik

system() invokes a new shell every time it is called.

 
$ cat abc.pl
#!/usr/bin/perl
print "-----------------\n";
system("cd test123");
system("pwd");
print "-----------------\n";
system("cd test123 && pwd") ;
print "-----------------\n";
 
 
$ perl abc.pl
-----------------
/tmp
-----------------
/tmp/test123
-----------------
 

Hello Since I do not see any other perl concepts in this, so why dont you go for a bash script rather than a perl?

Hi,

Thanks for the reply.

Your script works fine.

But, if you have seen my code, I have

#!/usr/bin/perl  

@dir_list = ` ls -d */. `;

 foreach $dir_name (@dir_list) {
          system("cd $dir_name");
          system("\rm -rf *.log");
          system("coreBuilder -shell -f cb_tests.tcl >& cb_tests.log");
          system("coreAssembler -shell -f ca_tests.tcl >& ca_tests.log");
          system("cd ../");
}

Here i wish to go in the directory through a variable. If I do something like,

system("cd $dir_name && pwd");

It gives an Error

sh: syntax error at line 2: `&&' unexpected

Thanks,
Hardik

---------- Post updated at 02:13 PM ---------- Previous update was at 02:10 PM ----------

Hey Gaurav,

What kind of bash you suggest??

How exactly it can be implemented by bash ??

Regards,
Hardik

you can do something like this in bash

 
#!/bin/bash
cd 8000294119
for dname in `ls -d */`
do
 echo $dname
 cd $dname
 rm -rf *.log
 coreBuilder -shell -f cb_tests.tcl >& cb_tests.log 
 coreAssembler -shell -f ca_tests.tcl >& ca_tests.log
 cd ..
done

hope this helps.

Hi,

I don't know if you observed or not, I am running a loop. In this loop I am repeating this activity in all the folders.

I am sure that would be tedious to do it through bash.

Let me know.

If you go through the code. you can see

 
#!/bin/bash
cd 8000294119
for dname in `ls -d */`   <=== for loop starts here
do
echo $dname
cd $dname
rm -rf *.log
coreBuilder -shell -f cb_tests.tcl >& cb_tests.log 
coreAssembler -shell -f ca_tests.tcl >& ca_tests.log
cd ..
done                         <=== for loop ends here

statements orange are executed within the loop.
dname is a variable which is a directory name and changes for each iteration of the loop.