[R] recovering "status" after q(status=n) in Windows
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Nov 27 14:25:57 CET 2007
As one more data point
http://blogs.msdn.com/powershell/archive/2006/09/15/ErrorLevel-equivalent.aspx
says $LASTEXITCODE is the equivalent in Windows PowerShell.
And for the cmd.exe shell:
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
says
%ERRORLEVEL% Returns the error code of the most recently used command.
A non zero value usually indicates an error.
Whereas
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/if.mspx?mfr=true
calls it the 'exit code'.
On Tue, 27 Nov 2007, Prof Brian Ripley wrote:
> On Mon, 26 Nov 2007, Dennis Fisher wrote:
>
>> Colleagues,
>>
>> I am writing scripts that run on both Windows (Vista) and Linux
>> (RedHat 9). The scripts will be used in both S-Plus (version 8) and
>> R (2.6.0). I am trying to use the "status" portion of the quit command:
>> ------------------------------------------------------------------------
>> -----------------------------------------------------------------------
>>> quit package:base R Documentation
>>>
>>> Terminate an R Session
>>>
>>> Description:
>>>
>>> The function 'quit' or its alias 'q' terminate the current R
>>> session.
>>>
>>> Usage:
>>>
>>> quit(save = "default", status = 0, runLast = TRUE)
>>> q(save = "default", status = 0, runLast = TRUE)
>>> .Last <- function(x) { ...... }
>>>
>>> Arguments:
>>>
>>> save: a character string indicating whether the environment
>>> (workspace) should be saved, one of '"no"', '"yes"', '"ask"'
>>> or '"default"'.
>>>
>>> status: the (numerical) error status to be returned to the operating
>>> system, where relevant. Conventionally '0' indicates
>>> successful completion.
>>>
>>>
>>>
>>>
>>> Some error statuses are used by R itself. The default error
>>> handler for non-interactive use effectively calls 'q("no", 1,
>>> FALSE)' and returns error code 1. Error status 2 is used for R
>>> 'suicide', that is a catastrophic failure, and other small
>>> numbers
>>> are used by specific ports for initialization failures. It is
>>> recommended that users choose statuses of 10 or more.
>>>
>>> Valid values of 'status' are system-dependent, but '0:255' are
>>> normally valid.
>> ------------------------------------------------------------------------
>> -----------------------------------------------------------------------
>> Of the four combinations (R/S-Plus crossed with Windows/Linux), the
>> only combination that I cannot get to work properly is R/Windows.
>>
>> The code below from Linux/R:
>>>> q(status=34, "no")
>>> P2: 72> echo $status
>>> 34
>> shows the expected response.
>>
>> When I replicate that R code in Windows, then type:
>> 1. echo %status%: Windows echoes %status, indicating that the
>> environment variable status was never set
>> 2. echo %error level%: Windows echoes 1 (if i execute a normal quit
>> command, i.e., without setting status, Windows echoes 0).
>>
>> So, it appears that in Windows, %errorlevel% is the (undocumented)
>> equivalent of $status (Linux).
>
> Actually, $status is shell-specific: $? is more standard (it comes from
> sh, $status from tcsh). $status does not work in /bin/sh on Linux for me
> (and I don't think it should).
>
> And that %errorlevel% does contain the status on some Windows shells is
> documented in the shell documentation.
>
>> However, in Windows, the value
>> returned appears to be limited to either 0 (normal exit) or 1 (status
>> set to a non-zero value) despite the claim that "valid values ...
>> [are] 0:255 (as is the case in Linux).
>
> The claim is of course correct! E.g. in tcsh
>
>> q("no", status=34)
> [d:/R/svn/trunk/src/gnuwin32]% echo $status
> 34
>
> and in cmd.exe
>
>> q("no", status=34)
>
> D:\R\R-2.6.1\bin>echo %errorlevel%
> 34
>
> Note those examples are from Rterm: Rgui is a Windows GUI application and
> the shell seeems to receive an error status from the GUI interface layer.
> But then Rterm is intended for use in scripts, not Rgui.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list