Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I execute unix commands in perl script.? 3

Status
Not open for further replies.

vptl

Technical User
Jan 22, 2004
120
US
Hi,
I am a new to perl...starting to learn.
I am looking for alternative to exec unix shell command in perl script,. for example...I use to do in ksh..

'ls -l |awk '{print $4}' >> /tmp/o'
Thanks.
V
 
You can use either backticks ("`") or the "system()" function to call your shell commands in Perl
E.g.
Code:
`ls -l |awk '{print $4}' >> /tmp/o`;
or
Code:
system('ls -l |awk '{print $4}' >> /tmp/o');

But you might be better learning how to write perl code to do these tasks. Perl can read directories, read from pipelines, split your records up and pick out the fields as well as write output files if you need them.
You may, of course, find that once you write these things in perl that you no longer need to write so many temporary files.
In Unix, you can usually use "perldoc" to get access to the perl documentation.

Trojan
 
1.Using `command`

my $cmd = `ls -l \|awk '{print \$4}' \>> /tmp/o`;

2. Using system

$cmd=system ("clear");





dmazzini
GSM System and Telecomm Consultant

 
TrojanWarBlade may be you forgot to escape |,$ and >.

Cheers



dmazzini
GSM System and Telecomm Consultant

 
So if I use system(command) , I assume it will be independent of OS ?
 
The most popular ways are

exec

and as all mentioned

system


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
vpti,

Somehow I doubt that the commands you run in your "system()" function will be platform independant.


Trojan
 
A very comment from TrojanWarBlade :), may be vptl you could execute a command asking first for the operating system.....

($^O =~ /Win/ ) ? system("notepad.exe $file") : system( "/usr/dt/bin/dtpad -standAlone -noReadOnlyWarning -viewOnly $file \&" );

dmazzini
GSM System and Telecomm Consultant

 
Pipelines are another way that have not yet really been discussed.

Code:
open PIPE, "ls -l |awk '{print \$4}' |" or die "failed to open pipe";
while(<PIPE>) {
  chomp;
  # Process each line here
}
close PIPE;

I use this as a dumb example only since I would never recommend using "ls" or "awk" in a pipeline but it demonstrates the principle.

Trojan


 
Thanks all for their input.
I am still not haveing luck...please see
this is my line 51 in my code.(Running on solaris)
I tried with and w/o backslash before ">" redirection.

system(`/nas/bin/nas_fs -list | egrep -v 'root|name' | awk '{print $6}'|sed '/^$/d' \> /tmp/fs_list`);

The error I get is

Use of uninitialized value in concatenation (.) at /tmp/b line 51, <STDIN> line 3.
sed: -e expression #1, char 2: Unterminated address regex


Please let me know what is not correct here...I can run this on my shell works fine.
 
You must not mix backticks with the "system()" function.
You are effectively executing the shell and then treating the result as another shell command to execute.
Also, you must be very careful of perl's metacharacters. They will all need to be escaped if used in backticks (or double quotes).
The "$" character will be causing you problems.

To fix, replace the backticks with double quotes and escape the "$" characters with a backslash ("\$") in all places.
Alternatively, you can create your own quotes in perl so here we could do something like this:

Code:
system(q!/nas/bin/nas_fs -list | egrep -v 'root|name' | awk '{print $6}'|sed  '/^$/d' > /tmp/fs_list!);

The q introduces the (single) quote character and I chose "!" for this example so a simple example string is:
Code:
my $string = q!This is a string!;
(by way of explanation).

Since this is a user defined single quoted we do not have to worry about escaping metacharacters.


Trojan
 
Got it...learning....thanks all....
forgot the backslashs ! :) \PERL\\:\)
 
Try it, It was not tested...

my $cmd = qq(/nas/bin/nas_fs -list | egrep -v 'root|name' | awk '{print \$6}'|sed '/^$\\/d' \> /tmp/fs_list);

print "$cmd\n";



dmazzini
GSM System and Telecomm Consultant

 
Thanks a lot Trojan..this is great point...I thought I got it...but what you mentioned is very good. perfect ...
Once again thank you all for great input...

GOnce again great teaching Trojan..keep it up.Thanks.
 
Trojan's earlier point is well made - perl has facilities for doing many of the things you are shelling out to other programs to get. The difference being that (in most cases, anyway) that the perl functions are platform-independent. So if your script needs to be portable, try to use the perl functions where possible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top