2015-12-23 42 views
0

我有這個腳本在Windows cmd文件,它做我想做的。它還顯示並記錄其輸出,但顯然該腳本中使用的程序的輸出不會按順序顯示和記錄。 請參閱下面的腳本和輸出。cmd /批處理文件沒有顯示,也沒有按順序記錄其他程序的輸出

@ECHO OFF 
:: script global variables 
SET log=nslookup-tests.log 

::Logic 
set LF=^ 



REM The two empty lines abive are required!! 
set output=****%date% %time% -- NSLOOKUP from anywhere with DNS server from edpnet**** 
setlocal EnableDelayedExpansion 

set output=!output!!LF!!LF!!LF!**youtube.com 
set output=!output!!LF!!LF!*edpnet DNS 
for /F "delims=" %%f in ('nslookup youtube.com 212.71.0.33') do (
set output=!output!!LF!%%f 
) 
set output=!output!!LF!!LF!*google DNS 
for /F "delims=" %%f in ('nslookup youtube.com 8.8.8.8') do (
set output=!output!!LF!%%f 
) 

set output=!output!!LF!!LF!!LF!**bestofyoutube.com 
set output=!output!!LF!!LF!*edpnet DNS 
for /F "delims=" %%f in ('nslookup bestofyoutube.com 212.71.0.33') do (
set output=!output!!LF!%%f 
) 
set output=!output!!LF!!LF!*google DNS 
for /F "delims=" %%f in ('nslookup bestofyoutube.com 8.8.8.8') do (
set output=!output!!LF!%%f 
) 

set output=!output!!LF!!LF!!LF!**vidzi.tv 
set output=!output!!LF!!LF!*edpnet DNS 
for /F "delims=" %%f in ('nslookup vidzi.tv 212.71.0.33') do (
set output=!output!!LF!%%f 
) 
set output=!output!!LF!!LF!*google DNS 
for /F "delims=" %%f in ('nslookup vidzi.tv 8.8.8.8') do (
set output=!output!!LF!%%f 
) 

::output to screen and append to log file 
echo !output! 
echo !output!!LF!!LF!!LF!!LF! >> "!log!" 

::Keep window open 
cmd /k 

輸出:

****wo 23/12/2015 9:46:18,94 -- NSLOOKUP from anywhere with DNS server from edpnet**** 

Non-authoritative answer: 
Non-authoritative answer: 
Non-authoritative answer: 
Non-authoritative answer: 
Non-authoritative answer: 
Non-authoritative answer: 
****do 24/12/2015 14:49:32,90 -- NSLOOKUP from anywhere with DNS server from edpnet**** 


**youtube.com 

*edpnet DNS 
Server: dns01.edpnet.net 
Address: 212.71.0.33 
Name: youtube.com 
Addresses: 2a00:1450:4013:c00::5d 
    173.194.65.190 
    173.194.65.136 
    173.194.65.93 
    173.194.65.91 

*google DNS 
Server: google-public-dns-a.google.com 
Address: 8.8.8.8 
Name: youtube.com 
Addresses: 2a00:1450:400c:c07::5b 
    85.234.204.215 
    85.234.204.236 
    85.234.204.251 
    85.234.204.226 
    85.234.204.237 
    85.234.204.211 
    85.234.204.245 
    85.234.204.230 
    85.234.204.241 
    85.234.204.222 
    85.234.204.221 
    85.234.204.207 
    85.234.204.249 
    85.234.204.219 
    85.234.204.234 


**bestofyoutube.com 

*edpnet DNS 
Server: dns01.edpnet.net 
Address: 212.71.0.33 
Name: bestofyoutube.com 
Address: 104.238.110.149 

*google DNS 
Server: google-public-dns-a.google.com 
Address: 8.8.8.8 
Name: bestofyoutube.com 
Address: 104.238.110.149 


**vidzi.tv 

*edpnet DNS 
Server: dns01.edpnet.net 
Address: 212.71.0.33 
Name: vidzi.tv 
Addresses: 46.17.100.122 
    46.28.202.183 
    82.115.15.12 

*google DNS 
Server: google-public-dns-a.google.com 
Address: 8.8.8.8 
Name: vidzi.tv 
Addresses: 82.115.15.12 
    46.28.202.183 
    46.17.100.122 

輸出的次序是有點有序的,但也有出現在頂部,而不是在日誌文件中的Non-authoritative answer:一堆。它們不會被追加到輸出變量中。

程序是否在訂單後執行,而不等待它完成?這看起來像異步行爲。

回答

1

stdout標準輸出消息和stderr錯誤消息。

Non-authoritative answer是寫入stderr的錯誤消息。

命令對於默認情況下只處理寫入標準輸出的消息

這將是也許更容易using command redirection operators和重定向標準輸出標準錯誤直接登錄,而不是收集所有消息系在環境變量文件終於寫出環境變量日誌文件的價值。

然而,重定向在每個stderr的NSLOOKUP標準輸出使用2>&1與逸出角支架和符號與^得到2>&1循環施加到NSLOOKUP代替命令將解決你的問題。

@echo off 
:: script global variables 
set log=nslookup-tests.log 

::Logic 
set LF=^ 



REM The two empty lines above are required!! 

set output=****%date% %time% -- NSLOOKUP from anywhere with DNS server from edpnet**** 
setlocal EnableDelayedExpansion 

set output=!output!!LF!!LF!!LF!**youtube.com 
set output=!output!!LF!!LF!*edpnet DNS 
for /F "delims=" %%f in ('nslookup youtube.com 212.71.0.33 2^>^&1') do (
    set output=!output!!LF!%%f 
) 
set output=!output!!LF!!LF!*google DNS 
for /F "delims=" %%f in ('nslookup youtube.com 8.8.8.8 2^>^&1') do (
    set output=!output!!LF!%%f 
) 

set output=!output!!LF!!LF!!LF!**bestofyoutube.com 
set output=!output!!LF!!LF!*edpnet DNS 
for /F "delims=" %%f in ('nslookup bestofyoutube.com 212.71.0.33 2^>^&1') do (
    set output=!output!!LF!%%f 
) 
set output=!output!!LF!!LF!*google DNS 
for /F "delims=" %%f in ('nslookup bestofyoutube.com 8.8.8.8 2^>^&1') do (
    set output=!output!!LF!%%f 
) 

set output=!output!!LF!!LF!!LF!**vidzi.tv 
set output=!output!!LF!!LF!*edpnet DNS 
for /F "delims=" %%f in ('nslookup vidzi.tv 212.71.0.33 2^>^&1') do (
    set output=!output!!LF!%%f 
) 
set output=!output!!LF!!LF!*google DNS 
for /F "delims=" %%f in ('nslookup vidzi.tv 8.8.8.8 2^>^&1') do (
    set output=!output!!LF!%%f 
) 

::output to screen and append to log file 
echo !output! 
echo !output!!LF!!LF!!LF!!LF! >> "!log!" 

::Keep window open 
%SystemRoot%\System32\cmd.exe /k 

這裏是你的代碼重定向標準錯誤標準輸出直接到日誌文件。

@echo off 
:: script global variables 
set "log=nslookup-tests.log" 

(
    echo ****%date% %time% -- NSLOOKUP from anywhere with DNS server from edpnet**** 
    echo. 
    echo. 
    echo **youtube.com 
    echo. 
    echo *edpnet DNS 
    %SystemRoot%\System32\nslookup.exe youtube.com 212.71.0.33 
    echo *google DNS 
    %SystemRoot%\System32\nslookup.exe youtube.com 8.8.8.8 

    echo. 
    echo **bestofyoutube.com 
    echo. 
    echo *edpnet DNS 
    %SystemRoot%\System32\nslookup.exe bestofyoutube.com 212.71.0.33 
    echo *google DNS 
    %SystemRoot%\System32\nslookup.exe bestofyoutube.com 8.8.8.8 

    echo. 
    echo **vidzi.tv 
    echo. 
    echo *edpnet DNS 
    %SystemRoot%\System32\nslookup.exe vidzi.tv 212.71.0.33 
    echo *google DNS 
    %SystemRoot%\System32\nslookup.exe vidzi.tv 8.8.8.8 
    echo. 
) >"%TEMP%\%log%" 2>&1 

rem Output to screen and append new log to previous log. 
type "%TEMP%\%log%" 
type "%TEMP%\%log%">>"%log%" 
del "%TEMP%\%log%" 

::Keep window open 
%SystemRoot%\System32\cmd.exe /k 
相關問題