Shell Command execution through PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Command execution through PERL
# 1  
Old 01-07-2010
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

Code:
#!/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

Last edited by pludi; 01-07-2010 at 03:46 AM.. Reason: code tags, please...
# 2  
Old 01-07-2010
system() invokes a new shell every time it is called.

Code:
 
$ 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
-----------------

# 3  
Old 01-07-2010
Quote:
Originally Posted by hardik_snps
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

Code:
#!/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
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?
# 4  
Old 01-07-2010
Hi,

Thanks for the reply.

Your script works fine.

But, if you have seen my code, I have
Code:
#!/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,
Code:
system("cd $dir_name && pwd");

It gives an Error
Code:
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

Last edited by Scott; 01-07-2010 at 04:52 AM.. Reason: Code tags, PLEASE!
# 5  
Old 01-07-2010
you can do something like this in bash

Code:
 
#!/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.
# 6  
Old 01-07-2010
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.
# 7  
Old 01-07-2010
Wrench

Quote:
Originally Posted by hardik_snps
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

Code:
 
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use shell command on Perl scritps?

On our OS I don't have Perl df command. How can I use shell commands from Perl scripts? (6 Replies)
Discussion started by: digioleg54
6 Replies

2. Shell Programming and Scripting

Usage of shell variable in perl command

Hi, I have a shell script, In which i have variable "var1" and some perl command inside shell script. export var1='coep -n rst-a2p-hinje.vci.all.com -c' perl -pi -e 's/^/coep -n rst-a2p-hinje.vci.all.com -c /' command.txt currently I am adding value of var1 in command.txt file by... (2 Replies)
Discussion started by: rakeshtomar82
2 Replies

3. Shell Programming and Scripting

Use shell variable in perl command line

Hi, I would like to use a shell variable $amp in my perl command line. for fa in $WORKSPACE/*.fa; do amp=`grep ">.*" $fa | sed -e's#>\(.*\)#\1#g'` ampsam="$WORKSPACE/$base/$base.$amp.sam" sqheader=`grep "^@SQ.*SN:$amp.*" $sam` printf "$sqheader\n" >> $ampsam ... (3 Replies)
Discussion started by: jdilts
3 Replies

4. Shell Programming and Scripting

Shell command execution always giving zero as return value

Whenever the shell script is invoked by the scheduler the command execution return code is always captured as 0(Success). If the same shell script is executed in command line via unix terminal, the command execution return code's are captured properly. For example: ls -ltr es_wrong_file ---->... (5 Replies)
Discussion started by: vemal
5 Replies

5. Shell Programming and Scripting

Passing perl variable to shell command

Can we pass perl variable to shell commands. If yes, please give some example. (2 Replies)
Discussion started by: Anjan1
2 Replies

6. Shell Programming and Scripting

ps command through shell and perl

Hi , I have a ps command inside perl script : ps -ef | egrep tail_file.pl | egrep -v egrep | awk '{print $2}' | egrep $pid through shell it's working fine . The same coammnd when executed through PERL , it'd displaying all the results oracle 10141 1 0 2010 ? ... (5 Replies)
Discussion started by: talashil
5 Replies

7. UNIX for Dummies Questions & Answers

perl command in shell not working

perl script i am using for search and replace is not working as expected... I am using it for in-place search/replace. I am new to perl, can someone please help me solving the issue here. ########SCRIPT #!/bin/sh var="abc" var2="def" perl -pi -e 's/${var}/def/g' testfile exit 0 ... (0 Replies)
Discussion started by: manishma71
0 Replies

8. Shell Programming and Scripting

running shell command in Perl script

Does not work. #!/usr/bin/perl $etcdir = 'ls -l /etc'; print $etcdir; #END ------------result-------- #perl -w abc123.pl ls -l /etc # This method works. #!/usr/bin/perl $etcdir = system("ls -l /etc"); print $etcdir; #END (2 Replies)
Discussion started by: dplinux
2 Replies

9. UNIX for Advanced & Expert Users

Invoking shell script with perl command.

Hi All, I am using the following command to invoke the shell script from a perl command. perl -i.bak -pe'BEGIN { $cmd = "/opt/coreservices/tomcat-5.5.9/bin/digest.sh -a sha"; } s/(password=")(*)/ $1.`$cmd $2|cut -d: -f2|tr -d "\n"` /e ' $CATALINA_HOME/conf/tomcat-users.xml I need... (1 Reply)
Discussion started by: nua7
1 Replies

10. Shell Programming and Scripting

perl command help in shell script

Hello friends , I need a help how to call the variable which has define in the shell script in the perl command line. example of the shell scrip : str="hell0" i want print the value of this variable in the perl command line. perl -e 'print $str' but this is not working . can... (2 Replies)
Discussion started by: venky_2_2000
2 Replies
Login or Register to Ask a Question