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
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. | [reply] |
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. | [reply] [d/l] |
|
system('cd ..;ls');
| [reply] [d/l] [select] |
|
| [reply] |
Re: Perl on cygwin
by aquarium (Curate) on Nov 19, 2007 at 05:32 UTC
|
| [reply] |
|
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.
| [reply] |
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
| [reply] |
|