2013-10-25 7 views
6

Seguito da questo argomento on here, sto cercando di capire cosa sta succedendo "dietro le quinte" da una risposta suggerita. Non capisco cosa dovrebbe fare 2> nul o 1> nul. E ho provato a decifrare cosa stanno facendo i simboli nella riga di avvio/b, ma qui sono davvero all'oscuro. Ho bisogno di un approccio graduale a quello se non ti dispiace.Informazioni su start, 2> nul, cmd e altri simboli in un file batch

Cosa sta succedendo in questa parte del codice?

2>nul del %lock%!nextProc! 
    %= Redirect the lock handle to the lock file. The CMD process will  =% 
    %= maintain an exclusive lock on the lock file until the process ends. =% 
    start /b "" cmd /c %lockHandle%^>"%lock%!nextProc!" 2^>^&1 !cpu%%N! !cmd! 
) 
    set "launch=" 

E questo:

) 9>>"%lock%%%N" 
) 2>nul 
    if %endCount% lss %startCount% (
    1>nul 2>nul ping /n 2 ::1 
    goto :wait 
) 

2>nul del %lock%* 

Copia del codice suggerito in piena:

@echo off 
setlocal enableDelayedExpansion 

:: Display the output of each process if the /O option is used 
:: else ignore the output of each process 
if /i "%~1" equ "/O" (
    set "lockHandle=1" 
    set "showOutput=1" 
) else (
    set "lockHandle=1^>nul 9" 
    set "showOutput=" 
) 

:: Define the maximum number of parallel processes to run. 
:: Each process number can optionally be assigned to a particular server 
:: and/or cpu via psexec specs (untested). 
set "maxProc=8" 

:: Optional - Define CPU targets in terms of PSEXEC specs 
::   (everything but the command) 
:: 
:: If a cpu is not defined for a proc, then it will be run on the local machine. 
:: I haven't tested this feature, but it seems like it should work. 
:: 
:: set cpu1=psexec \\server1 ... 
:: set cpu2=psexec \\server1 ... 
:: set cpu3=psexec \\server2 ... 
:: etc. 

:: For this demo force all cpu specs to undefined (local machine) 
for /l %%N in (1 1 %maxProc%) do set "cpu%%N=" 

:: Get a unique base lock name for this particular instantiation. 
:: Incorporate a timestamp from WMIC if possible, but don't fail if 
:: WMIC not available. Also incorporate a random number. 
    set "lock=" 
    for /f "skip=1 delims=-+ " %%T in ('2^>nul wmic os get localdatetime') do (
    set "lock=%%T" 
    goto :break 
) 
    :break 
    set "lock=%temp%\lock%lock%_%random%_" 

:: Initialize the counters 
    set /a "startCount=0, endCount=0" 

:: Clear any existing end flags 
    for /l %%N in (1 1 %maxProc%) do set "endProc%%N=" 

:: Launch the commands in a loop 
    set launch=1 
    echo mem=1m 2m 3m 4m 6m 8m 12m 16m 24m 32m 48m 64m 96m 128m 192m 256m 384m 512m 768m 1024m 
    echo o=2 3 4 5 6 7 8 10 12 14 16 20 24 28 32 
    echo s=off 1m 2m 4m 8m 16m 32m 64m 128m 256m 512m 1g 2g 4g 8g 16g 32g 64g on 
    echo x=1 3 5 7 9 
    for %%x IN (9) DO for %%d IN (1024m 768m 512m 384m 256m 192m 128m 96m 64m 48m 32m 24m 16m 12m 8m 6m 4m 3m 2m 1m) DO (
    set "cmd=7z.exe a teste.resultado\%%xx.ppmd.%%dd.%%ww.%%ss.7z .\teste.original\* -mx=%%x -m0=PPMd:mem=%%d:o=%%w -ms=%%s" 
    if !startCount! lss %maxProc% (
     set /a "startCount+=1, nextProc=startCount" 
    ) else (
     call :wait 
    ) 
    set cmd!nextProc!=!cmd! 
    if defined showOutput echo ------------------------------------------------------------------------------- 
    echo !time! - proc!nextProc!: starting !cmd! 
    2>nul del %lock%!nextProc! 
    %= Redirect the lock handle to the lock file. The CMD process will  =% 
    %= maintain an exclusive lock on the lock file until the process ends. =% 
    start /b "" cmd /c %lockHandle%^>"%lock%!nextProc!" 2^>^&1 !cpu%%N! !cmd! 
) 
    set "launch=" 

:wait 
:: Wait for procs to finish in a loop 
:: If still launching then return as soon as a proc ends 
:: else wait for all procs to finish 
    :: redirect stderr to null to suppress any error message if redirection 
    :: within the loop fails. 
    for /l %%N in (1 1 %startCount%) do (
    %= Redirect an unused file handle to the lock file. If the process is =% 
    %= still running then redirection will fail and the IF body will not run =% 
    if not defined endProc%%N if exist "%lock%%%N" (
     %= Made it inside the IF body so the process must have finished =% 
     if defined showOutput echo =============================================================================== 
     echo !time! - proc%%N: finished !cmd%%N! 
     if defined showOutput type "%lock%%%N" 
     if defined launch (
     set nextProc=%%N 
     exit /b 
    ) 
     set /a "endCount+=1, endProc%%N=1" 
    ) 9>>"%lock%%%N" 
) 2>nul 
    if %endCount% lss %startCount% (
    1>nul 2>nul ping /n 2 ::1 
    goto :wait 
) 

2>nul del %lock%* 
if defined showOutput echo =============================================================================== 
echo Thats all folks! 
+0

:: 1 sembra un commento per me – djangofan

risposta

16

La cifra prima di un simbolo di reindirizzamento è il numero torrente per reindirizzare.
Il flusso predefinito è 1, quando nessun numero è presente, quindi 1>... e >... sono equivalenti.

stream 1 è il flusso di input/output standard, 2 è il flusso di errore standard.

Un comando può essere inviato a più flussi ed è consentito reindirizzare ciascuno di essi a una destinazione diversa.

Quindi 2>nul e 1>nul ha semplicemente affermato che l'output dell'errore e l'output normale verranno reindirizzati su nul. Quindi non verrà emesso nulla.

0

1> nul e 2> nul fanno in modo che non venga visualizzato alcun output.
il ^> all'inizio sono così> sono passati al comando di avvio, non interpretati. il cmd/c inizia una nuova shell che esegue il codice dopo il/c e quindi esce.

2

La mia spiegazione:

1. 2>nul del %lock%!nextProc! 
2.  %= Redirect the lock handle to the lock file. The CMD process will  =% 
3.  %= maintain an exclusive lock on the lock file until the process ends. =% 
4.  start /b "" cmd /c %lockHandle%^>"%lock%!nextProc!" 2^>^&1 !cpu%%N! !cmd! 
5. ) 
6. set "launch=" 

Linea 1: cancellare il file e non mostrare gli errori. come "del/Q". i simboli esclamativi richiedono che l'espansione ritardata sia abilitata per valutare qualsiasi cosa. Vorrei averlo scritto come: del/Q "% lock%! NextProc!"

Linea 2: uno stile di commento davvero strano. dovrebbe solo iniziare ogni riga con "::" invece

Linea 3: stessa linea 2

Linea 4: difficile dire senza vedere resto dello script. il %% N indica che questa sezione si trova all'interno di un blocco di loop. i caratteri^sono necessari in modo che il comando di avvio riconosca i caratteri speciali come parte della stringa di comando cmd. Non credo che fosse necessario il comando di avvio qui IMHO. Scommetto che "start/B/wait" è l'equivoco di "start/b" "cmd/c". Vorrei riscrivere questo script personalmente per renderlo più facile da capire.

Inoltre, vedere dostips.com

anche: 1> nul 2> nul ping/n 2 :: 1 è l'equivilant di "ping -n 2 w 1000 127,1> nul", ma stupidamente più difficile da capire .

anche:% ~ 1 significa per ottenere il primo arg% 1 e tagliare le virgolette (se presente)

Potrei continuare a lungo, ma si deve solo la ricerca da soli.

Problemi correlati