Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl on cygwin

by thunderbird (Initiate)
on Nov 19, 2007 at 05:16 UTC ( [id://651588]=perlquestion: print w/replies, xml ) Need Help??

thunderbird has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a relatively simple but frustrating question. I need to call various system commands like 'cat' from a Perl script. I find that commands like 'mkdir' work fine, however when I run the command 'system(cd somedirectory)' I get an error saying file or directory not found. This happens no matter which directory I try and cd to. After lots of searching i discovered that cygwin calls the Windows cd command when used via the 'system' call. I tried putting the cygwin bin path in my windows path but that didn't help either. Any help on this would be most appreciated. Thanks, Thunderbird

Replies are listed 'Best First'.
Re: Perl on cygwin
by ysth (Canon) on Nov 19, 2007 at 05:34 UTC
    If you want to change the working directory, use chdir. Using system, even if it worked, would just change it in the child process, which would do you no good.
Re: Perl on cygwin
by cdarke (Prior) on Nov 19, 2007 at 12:59 UTC
    ysth is correct, using cd will only change the child process, regardless of Cygwin. However, on the assumption that you are using another command which follows (you don't actually say what you are trying to do) then the following works on Cygwin:
    system('sh -c "cd ..;ls"');

    This invokes the Bourne shell to execute its built-in cd command.

    update: 'which cd' gives a similar message on bash and ksh on Linux ('no cd in ...') because cd is a shell built-in. Try 'type cd' instead'

    If all you want to do is cat(1) there are very easy ways of doing that in Perl without invoking another program.
      system('sh -c "cd ..;ls"');

      That is better written as:

      system('cd ..;ls');

      - tye        

        Yes, it does save one process besides being shorter. Still it could give wrong results if cd fails. A true shell hacker ;) would write system('cd ... && ls') (would even use 'ls -1' to avoid terminal issues).

        cheers --stephan
Re: Perl on cygwin
by aquarium (Curate) on Nov 19, 2007 at 05:32 UTC
    use forward slashes in the path and double quote " entire path.
    the hardest line to type correctly is: stty erase ^H
      Hi aquarium, I have been using forward slashes and double quotes, however the problem seems to be the fact that Perl on cygwin cannot find the "cd" command. In fact if you do a "which cd" from the command line in Cygwin, you get the result "cd: Command not found", whereas this works for commands like "mkdir". Apparently the cd command from system in cygwin uses the Windows cd and this is the source of the problem I'm experiencing.
Re: Perl on cygwin
by thunderbird (Initiate) on Nov 20, 2007 at 07:18 UTC
    Thanks a lot for all your useful comments guys. Regards, Thunderbird

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://651588]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found