2009-08-13 30 views
49

È possibile uscire da PowerShell digitando exit. Fin qui tutto bene. Ma cos'è esattamente questo?Che cosa è esattamente "uscita" in PowerShell?

PS Home:\> gcm exit 
Get-Command : The term 'exit' is not recognized as the name of a cmdlet, function, script file, or operable program. Ch 
eck the spelling of the name, or if a path was included, verify that the path is correct and try again. 
At line:1 char:4 
+ gcm <<<< exit 
    + CategoryInfo   : ObjectNotFound: (exit:String) [Get-Command], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand 

Quindi è né un cmdlet, una funzione, uno script o un programma. Lascia la domanda che cosa è esattamente.

Questo significa, purtroppo, anche che non si può creare alias per exit:

PS Home:\> New-Alias ^D exit 
PS Home:\> ^D 
Cannot resolve alias '♦' because it refers to term 'exit', which is not recognized as a cmdlet, function, operable prog 
ram, or script file. Verify the term and try again. 
At line:1 char:2 
+ ♦ <<<< 
    + CategoryInfo   : ObjectNotFound: (♦:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : AliasNotResolvedException 

Ci sono più di tali comandi che sono comandi?

ETA: Solo per riferimento: so che posso semplicemente racchiuderlo in una funzione. Il mio profilo ha le linee

# exit with Ctrl+D 
iex "function $([char]4) { exit }" 

in loro. La mia domanda era solo per sapere che cosa esattamente questo comando è.

risposta

65

È una parola chiave riservata (come ritorno, filtro, funzione, interruzione).

Reference

Inoltre, ai sensi dell'articolo 7.6.4 del Powershell in Action di Bruce Payette:

Ma cosa succede quando si desidera uno script per uscire dall'interno di una funzione definita in quello script? ... Per semplificare l'operazione, Powershell ha la parola chiave di uscita.

Naturalmente, come altri hanno fatto notare, non è difficile da fare ciò che si vuole avvolgendo l'uscita in una funzione:

PS C:\> function ex{exit} 
PS C:\> new-alias ^D ex 
+3

Secondo 'Get-Help Exit-PSSession' _You può anche utilizzare la parola chiave Exit per terminare una sessione interattiva. L'effetto è lo stesso dell'utilizzo di Exit-PSSession._ Quindi dice anche che "Exit" è una parola chiave. Tuttavia, sembra che la citazione non possa essere presa per implicare che 'Exit' e' Exit-PSSession' sono equivalenti in tutti i casi. La parola chiave exit può essere utilizzata con un argomento che può essere convertito in '[int]', e in tal caso quel numero sarà il codice di uscita. Ad esempio "Uscita 42". –