tags:

views:

95

answers:

4

I’m writing a LaTeX package which needs to use \write18. Some of the shell commands I issue are system-specific (e.g. rm vs. del). Is there a way to determine what system I’m running on?

It would be enough to disambiguate between Windows and other (Unix-like) systems.

+1  A: 

Not very good but it works for me

\newread\checkf
\immediate\openin\checkf = C:/WINDOWS/win.ini
\ifeof\checkf not windows \else windows\fi
\closein\checkf
Alexey Malistov
+1  A: 

If you can mark your OS with a file you can do

\IfFileExists{/etc/motd}{unix code here}{windows code here}

There's nothing special about the path /etc/motd; it's just likely to be found on a Unix system and unlikely on a Windows system. If you want to be dead certain, you should create a file specifically to mark the system in whatever way you want to identify it.

Norman Ramsey
The idea is similar to Alexey’s and it’s pretty good. But I have no control over the system and yes, I’d prefer to be dead certain since this will be in a package that is publicly distributed. ;-)
Konrad Rudolph
A: 

A friend of mine had the following idea which I’m now using. I’ve only tested it on Windows XP and OS X where it works fine. A bit flimsy on testing, admittedly, but in principle it should work fine almost anywhere else.

\newif\ifwindows

\immediate\write18{echo $SHELL > \jobname.os}
\newread\@whichos
\immediate\openin\@whichos\jobname.os
\read\@whichos to \@whichosshell
\ifthenelse{\equal{\@whichosshell}{$SHELL }}
 {\windowstrue}
 {\windowsfalse}
\closein\@whichos

\ifwindows
  \typeout{System detected: Windows}
  \newcommand\DeleteFile[1]{\immediate\write18{del #1}}
\else
  \typeout{System detected: Unix-like}
  \newcommand\DeleteFile[1]{\immediate\write18{rm #1}}
\fi
% Cleanup.
\DeleteFile{\jobname.os}

The key here is that Windows won’t expand the $SHELL environment variable (any other variable would have done, really) so it will write the string $SHELL to the file literally.

Konrad Rudolph
+2  A: 

Take a look at the LaTeX ifplatform package. There was a lot of discussion about reliable methods across a range of platforms, and the current release works very well.

Joseph Wright
Nice. It uses `\IfFileExists` with `/dev/null` for Unix systems and `nul:` for Windows. And a whole bunch of wacky fallbacks if that goes wrong. +1
Norman Ramsey