Set output of a command as a variable (with pipes)

前端 未结 6 1117
花落未央
花落未央 2020-12-01 05:09

Can you redirect the output of a command to a variable with pipes?

I haven\'t tried much as I haven\'t been able to think of anything to try, but I have tried one me

6条回答
  •  醉酒成梦
    2020-12-01 05:54

    THIS DOESN'T USE PIPEs, but requires a single tempfile
    I used this to put simplified timestamps into a lowtech daily maintenance batfile

    We have already Short-formatted our System-Time to HHmm, (which is 2245 for 10:45PM)
    I direct output of Maint-Routines to logfiles with a $DATE%@%TIME% timestamp;
    . . . but %TIME% is a long ugly string (ex. 224513.56, for down to the hundredths of a sec)

    SOLUTION OVERVIEW:
    1. Use redirection (">") to send the command "TIME /T" everytime to OVERWRITE a temp-file in the %TEMP% DIRECTORY
    2. Then use that tempfile as the input to set a new variable (I called it NOW)
    3. Replace

    echo $DATE%@%TIME% blah-blah-blah >> %logfile%
          with
    echo $DATE%@%NOW% blah-blah-blah >> %logfile%


    ====DIFFERENCE IN OUTPUT:
    BEFORE:
    SUCCESSFUL TIMESYNCH 29Dec14@222552.30
    AFTER:
    SUCCESSFUL TIMESYNCH 29Dec14@2252


    ACTUAL CODE:

    TIME /T > %TEMP%\DailyTemp.txt
    SET /p NOW=<%TEMP%\DailyTemp.txt
    echo $DATE%@%NOW% blah-blah-blah >> %logfile%


    AFTERMATH:
    All that remains afterwards is the appended logfile, and constantly overwritten tempfile. And if the Tempfile is ever deleted, it will be re-created as necessary.

提交回复
热议问题