CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Calling system() on Cygwin/Windows

 



Paveltc
New User

Oct 10, 2011, 10:59 AM

Post #1 of 5 (1281 views)
Calling system() on Cygwin/Windows Can't Post

Hello everyone,

I'm new to this forum and I'm sorry if this is a common question. I am writing a Perl script in the Windows environment but I am using Cygwin. I'm not going to be the only one using this script and I don't know if people will be using the Cygwin environment when executing it or if they will be using Windows cmd.exe. I need to make several system() calls from my script and I was wondering if there was a way to check what environment the user is calling the script from. That way I could set up an if statement that would do system ("rm test.txt") if it's cygwin or system ("del test.txt") if it's cmd.exe.

Thank You for the help,

Pavel


FishMonger
Veteran / Moderator

Oct 10, 2011, 5:16 PM

Post #2 of 5 (1270 views)
Re: [Paveltc] Calling system() on Cygwin/Windows [In reply to] Can't Post

Neither of those 2 system calls are needed. Perl has an unlink function which is platform independent.

See: http://perldoc.perl.org/functions/unlink.html

I haven't used Cygwin for some time so I'm not sure if it alters the $^O ( $OSNAME ) var, but you could check that out, or you could check the %ENV hash for the environment vars that Cygwin sets to make your decision on which system call to make.

In most cases, you should be able to use either a Perl builtin, such as unlink, or a platform independent module to accomplish your goal.


Paveltc
New User

Oct 10, 2011, 5:50 PM

Post #3 of 5 (1268 views)
Re: [FishMonger] Calling system() on Cygwin/Windows [In reply to] Can't Post

Thanks, that helps somewhat. I should have stated my question better, the deleting of a file was just one example of a system command that I need to call. I also need to call executable files and Cygwin requires that you have "./" before an executable. Also, Cygwin and cmd are different in that the first uses / to separate files and the latter uses \. So I'm wondering if there is a way to write the script so that it can find out which is being used and which commands to call having gained that knowledge.

Thanks again.


FishMonger
Veteran / Moderator

Oct 10, 2011, 6:52 PM

Post #4 of 5 (1262 views)
Re: [Paveltc] Calling system() on Cygwin/Windows [In reply to] Can't Post

Windows not only supports but uses / forward slashes for the directory separator at the system level. It's cmd.exe that "doesn't or may not like" forward slashes.

Here's a clear example where using forward slashes works.

Quote
C:\Users\Ron>cd c:/Windows

c:\Windows>


There are cases were you need to use quotes around the path/string, such as when calling some 3rd party executables, but Windows itself doesn't require them and when it does, you can escape them when passing the command to the system call.

Instead of dancing around hypothetical assumptions, you should post concrete examples that you're having trouble with so that we can show you how to deal with the situation.


rovf
Veteran

Oct 12, 2011, 3:51 AM

Post #5 of 5 (1193 views)
Re: [Paveltc] Calling system() on Cygwin/Windows [In reply to] Can't Post

In addition to what FishMonger said:

Some programs and CMD-builtin-commands *require* forward slashes (example: XCOPY.EXE or the DIR builtin command), and some allow it (example: less.exe from the GnuUtils and the CD builtin command).

If you do it wrong, you might or might not get an error message. For example, while


Code
xcopy \temp\a b


copies the files, the forward-slash variant


Code
xcopy /temp/a b


just silently fails.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives