NEWBIE: Perls System -command and Cygwin bash-shell

P

Pekka Niiranen

Hi there,

I am having problem in W2K when using Cygwin's Perl.
My Perl script starts Shell script with System -command;
system(scriptfile). The shell script creates a temporary file
like this:

#!/usr/bin/sh
cat "blah blah" > tempfile

and then exits back to Perl. When I then try to remove
the created "tempfile" from the same Perl script with
"unlink" I find out that Perl does not have access rights to the file.
If I use "readdir" Perl finds the file but nor command
"system(rm $file)" or "unlink($file)" does not work either. However,
when Perl script exits I can remove the file with
normal Bash shell command: "rm tempfile".

It seems that Perl is running with different user rights
than the Shell script. My question is therefore: "How can I remove file
created by the Bash shell script started as subshell from Perl script?"
Should I start another Shell script that executes "rm tempfile"
just for that task?

-pekka-
 
A

Anno Siegel

Pekka Niiranen said:
Hi there,

I am having problem in W2K when using Cygwin's Perl.
My Perl script starts Shell script with System -command;
system(scriptfile). The shell script creates a temporary file
like this:

#!/usr/bin/sh
cat "blah blah" > tempfile

and then exits back to Perl. When I then try to remove
the created "tempfile" from the same Perl script with
"unlink" I find out that Perl does not have access rights to the file.

So what *are* the permissions and ownership of the file? What
does "ls -l" say? You are not giving us vital information.
If I use "readdir" Perl finds the file but nor command
"system(rm $file)" or "unlink($file)" does not work either. However,

Does $file contain what you think it does?
when Perl script exits I can remove the file with
normal Bash shell command: "rm tempfile".

It seems that Perl is running with different user rights
than the Shell script. My question is therefore: "How can I remove file
created by the Bash shell script started as subshell from Perl script?"

No idea. Show complete code that demonstrates the problem.

Anno
 
A

A. Sinan Unur

and then exits back to Perl. When I then try to remove
the created "tempfile" from the same Perl script with
"unlink" I find out that Perl does not have access rights to the file.
If I use "readdir" Perl finds the file but nor command
"system(rm $file)" or "unlink($file)" does not work either. However,
when Perl script exits I can remove the file with
normal Bash shell command: "rm tempfile".

It seems that Perl is running with different user rights

I find that highly unlikely.
than the Shell script. My question is therefore: "How can I remove file
created by the Bash shell script started as subshell from Perl script?"
Should I start another Shell script that executes "rm tempfile"
just for that task?

In addition to Anno's suggestions, I have one question. Do you read from
the tempfile in your Perl script. If you do, does your script close the
file handle before attempting to call unlink?
 
P

pekka niiranen

Ok here goes,

---the perl script named "p" starts--
#!/usr/local/bin/perl -w
unless (opendir(TMPDIR, "/cygdrive/c/home/cygwin/tmp")) {
print "Can't open temporary directory !";
}
system("/cygdrive/c/home/cygwin/s"); # run the shell script
while( defined ($tmpfile = readdir TMPDIR) ) {
next if $tmpfile =~ /^\.\.?$/;
system("chmod 660 $tmpfile") or die;
}
system("ls -l ./tmp");
closedir(TMPDIR);

---the perl script named "p" stops---

---the bash script named "s" starts---

#!/usr/bin/bash
echo "Blah Blah" > ./tmp/tfile
exit
---the bash script named "s" stops---

The perl script and shell script are run from the same directory.
The file "tfile" is created into subdirectory "tmp"

"ls -l" from script directory gives (among other things):
drwxr-xr-x+ 2 treniira Administ 0 Jul 23 12:11 tmp/

Perl scripts output is:
[vat58008:~] $ ./p
chmod: getting attributes of `tfile': No such file or directory
total 1
-rw-r--r-- 1 treniira Administ 10 Jul 23 12:23 tfile


-pekka-
 
P

pekka niiranen

Got it,

"readdir" does not return the full path;
I must built the full path to the file when calling "system":
system("chmod 660 <add path here>/$tmpfile")

Thanks anyway,

-pekka-

pekka said:
Ok here goes,

---the perl script named "p" starts--
#!/usr/local/bin/perl -w
unless (opendir(TMPDIR, "/cygdrive/c/home/cygwin/tmp")) {
print "Can't open temporary directory !";
}
system("/cygdrive/c/home/cygwin/s"); # run the shell script
while( defined ($tmpfile = readdir TMPDIR) ) {
next if $tmpfile =~ /^\.\.?$/;
system("chmod 660 $tmpfile") or die;
}
system("ls -l ./tmp");
closedir(TMPDIR);

---the perl script named "p" stops---

---the bash script named "s" starts---

#!/usr/bin/bash
echo "Blah Blah" > ./tmp/tfile
exit
---the bash script named "s" stops---

The perl script and shell script are run from the same directory.
The file "tfile" is created into subdirectory "tmp"

"ls -l" from script directory gives (among other things):
drwxr-xr-x+ 2 treniira Administ 0 Jul 23 12:11 tmp/

Perl scripts output is:
[vat58008:~] $ ./p
chmod: getting attributes of `tfile': No such file or directory
total 1
-rw-r--r-- 1 treniira Administ 10 Jul 23 12:23 tfile


-pekka-

Anno said:
So what *are* the permissions and ownership of the file? What
does "ls -l" say? You are not giving us vital information.





Does $file contain what you think it does?





No idea. Show complete code that demonstrates the problem.

Anno
 
A

Anno Siegel

Please don't top-post. I moved your reply in context.

pekka niiranen said:
Ok here goes,

---the perl script named "p" starts--
#!/usr/local/bin/perl -w
unless (opendir(TMPDIR, "/cygdrive/c/home/cygwin/tmp")) {
print "Can't open temporary directory !";
}

Checking "opendir" for errors is good.
system("/cygdrive/c/home/cygwin/s"); # run the shell script

Not checking "system" for errors is not so good.
while( defined ($tmpfile = readdir TMPDIR) ) {
next if $tmpfile =~ /^\.\.?$/;
system("chmod 660 $tmpfile") or die;

That won't work, unless your current directory happens to be
/cygdrive/c/home/cygwin/tmp, where the file is. That's the
reason for your error. Instead use

chmod 0660, "/cygdrive/c/home/cygwin/tmp/$tmpfile" or die "chmod";

Note that I replaced the system call with Perl's internal chmod.
}
system("ls -l ./tmp");
closedir(TMPDIR);

[snip]

Anno
 
S

Sherm Pendley

Anno said:
Checking "opendir" for errors is good.

Yes, and reporting the cause of those errors is even better:

print "Can't open temporary dir: $!";

sherm--
 
J

Joe Smith

pekka said:
unless (opendir(TMPDIR, "/cygdrive/c/home/cygwin/tmp")) {
print "Can't open temporary directory !";
}
system("/cygdrive/c/home/cygwin/s"); # run the shell script
while( defined ($tmpfile = readdir TMPDIR) ) {

It is not a good idea to opendir a directory before creating a new
file in that directory. The readdir() may or may not see the new file.

Any time you opendir a directory other than '.', you must remember
to prepend the directory path to whatever readdir() returns.

system("/home/cygwin/s"); # Use the cygwin name not windows name
$dir = "/home/cygwin/tmp"; # Use the cygwin name not windows name
open(TMPDIR,$dir) or die "Cannot open directory $dir: $!";
while (defined ($dir_entry = readdir TMPDIR) ) {
$tmpfile = "$dir/$dir_entry";
...
}

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top