1838458 Members
3093 Online
110126 Solutions
New Discussion

Perl "system" command

 
SOLVED
Go to solution
Youlette Etienne_2
Regular Advisor

Perl "system" command

Hello everyone,

I am trying to run the following in a Perl program,

system("find /tmp -name $file_name -exec chown maestro:unison {} \;");

I get the following error message:

/tmp/security.log
sh[2]: -exec: not found.

How can I fix this?
If at first you don't succeed, change the rules!
5 REPLIES 5
Rodney Hills
Honored Contributor

Re: Perl "system" command

Since you are using quotes, the \ near the end of the line needs to be protected so that is gets passed.

Try-
system("find /tmp -name '$file_name' ... unison {} \\;");

I assume $file_name is a perl variable and has valid entries. The single quotes around it will be sure any funny named files don't confuse the shell.

HTH

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl "system" command

I beleive

1. $file_name has some space or other non-allowed character and should be quoted
2. You should not use system ()

--8<---
use File::Find;
my $uid = (getpwnam "maestro")[2];
my $gid = (getgrnam "unison")[2];
find (sub {
$_ eq $file_name or return;
chown $uid, $gid, $_;
}, "/tmp");
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Youlette Etienne_2
Regular Advisor

Re: Perl "system" command

Procura,

I used the following

use File::Find;
sub changed {
if ( $_ eq $file_name ) {
chown (104, 102, $_);
chmod 0777, $File::Find::name;
}; }

find(\&changed, '/tmp/logs');

which does not work. However, if I changed the line

if ( $_ eq $file_name ) {

to

if ( $_ eq "security.log" ) {

the actual file name, it works.
Enclosing $file_name in quotes,either double or single, do not work either. I am using Perl 5.8.4
Any suggestions?
If at first you don't succeed, change the rules!
Rodney Hills
Honored Contributor

Re: Perl "system" command

Where does "$file_name" get set?

If you read it from a file, remember to do a chomp on it to remove the trailing \n.

HTH

-- Rod Hills
There be dragons...
Youlette Etienne_2
Regular Advisor

Re: Perl "system" command

Yes, of course! That worked. Thanks so much!
If at first you don't succeed, change the rules!