Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > NEWBIE: Perls System -command and Cygwin bash-shell

Reply
Thread Tools

NEWBIE: Perls System -command and Cygwin bash-shell

 
 
Pekka Niiranen
Guest
Posts: n/a
 
      07-22-2004
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-

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      07-23-2004
Pekka Niiranen <> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-23-2004
Pekka Niiranen <> wrote in news:4100130f$0
$13046$:

> 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?

--
A. Sinan Unur
d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
pekka niiranen
Guest
Posts: n/a
 
      07-23-2004
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 Siegel wrote:
> Pekka Niiranen <> wrote in comp.lang.perl.misc:
>
>>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

 
Reply With Quote
 
pekka niiranen
Guest
Posts: n/a
 
      07-23-2004
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 niiranen wrote:

> 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 Siegel wrote:
>
>> Pekka Niiranen <> wrote in
>> comp.lang.perl.misc:
>>
>>> 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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-23-2004
Please don't top-post. I moved your reply in context.

pekka niiranen <> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Pekka Niiranen <> wrote in comp.lang.perl.misc:
> >
> >>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



> 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
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      07-23-2004
Anno Siegel wrote:
>
> pekka niiranen <> wrote in comp.lang.perl.misc:
>
>> print "Can't open temporary directory !";

>
> Checking "opendir" for errors is good.


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

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

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-25-2004
pekka niiranen wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Text File Encoding and converting to Perls internal UTF-8 encoding sln@netherlands.com Perl Misc 2 04-17-2009 11:22 PM
using target words from arrays in regex, pythons version of perls'map' Lance Hoffmeyer Python 6 05-17-2006 03:04 AM
HTML parsing as good as Perls. TLOlczyk Ruby 5 06-21-2005 09:18 PM
Perls system() call fails in a cgi-file running on win2k and apache Mr. Zeus Perl Misc 6 10-13-2004 07:52 PM
Emulate perls local Karsten Meier Ruby 2 09-09-2003 07:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57