NEWBIE: Perls System -command and Cygwin bash-shell

Discussion in 'Perl Misc' started by Pekka Niiranen, Jul 22, 2004.

  1. 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-
     
    Pekka Niiranen, Jul 22, 2004
    #1
    1. Advertising

  2. Pekka Niiranen

    Anno Siegel Guest

    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
     
    Anno Siegel, Jul 23, 2004
    #2
    1. Advertising

  3. 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)
     
    A. Sinan Unur, Jul 23, 2004
    #3
  4. Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details

    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
     
    pekka niiranen, Jul 23, 2004
    #4
  5. SOLUTION: NEWBIE: Perls System -command and Cygwin bash-shell, Moredetails

    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
     
    pekka niiranen, Jul 23, 2004
    #5
  6. Pekka Niiranen

    Anno Siegel Guest

    Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details

    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
     
    Anno Siegel, Jul 23, 2004
    #6
  7. Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details

    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
     
    Sherm Pendley, Jul 23, 2004
    #7
  8. Pekka Niiranen

    Joe Smith Guest

    Re: NEWBIE: Perls System -command and Cygwin bash-shell, More details

    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
     
    Joe Smith, Jul 25, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,331
    William Park
    Jul 14, 2005
  2. brianj
    Replies:
    3
    Views:
    7,852
    Default User
    Feb 11, 2004
  3. edwithad

    Bash shell to Python shell?

    edwithad, Sep 28, 2009, in forum: Python
    Replies:
    4
    Views:
    1,811
  4. Mr. Zeus
    Replies:
    6
    Views:
    290
    Ben Morrow
    Oct 13, 2004
  5. Peng Yu
    Replies:
    3
    Views:
    390
    Rainer Weikusat
    May 13, 2013
Loading...

Share This Page