zoukankan      html  css  js  c++  java
  • batch

    https://ss64.com/nt/

     https://stackoverflow.com/questions/14952295/set-output-of-a-command-as-a-variable-with-pipes

    The lack of a Linux-like backtick/backquote facility is a major annoyance of the pre-PowerShell world. Using backquotes via for-loops is not at all cosy. So we need kinda of setvar myvar cmd-line command.

    In my %path% I have a dir with a number of bins and batches to cope with those Win shortcomings.

    One batch I wrote is:

    :: setvar varname cmd
    :: Set VARNAME to the output of CMD
    :: Triple escape pipes, eg:
    :: setvar x  dir c: ^^^| sort 
    :: -----------------------------
    
    @echo off
    SETLOCAL
    
    :: Get command from argument 
    for /F "tokens=1,*" %%a in ("%*") do set cmd=%%b
    
    :: Get output and set var
    for /F "usebackq delims=" %%a in (`%cmd%`) do (
         ENDLOCAL
         set %1=%%a
    )
    
    :: Show results 
    SETLOCAL EnableDelayedExpansion
    echo %1=!%1! 
    

    So in your case, you would type:

    > setvar text echo Hello
    text=Hello 
    

    The script informs you of the results, which means you can:

    > echo text var is now %text%
    text var is now Hello 
    

    You can use whatever command:

    > setvar text FIND "Jones" names.txt
    

    What if the command you want to pipe to some variable contains itself a pipe?
    Triple escape it, ^^^|:

    > setvar text dir c: ^^^| find "Win"
  • 相关阅读:
    bzoj2438
    bzoj3040
    [AHOI2009]维护序列
    [JSOI2008]最大数
    洛谷3378堆模板
    洛谷1439
    洛谷2756
    bzoj1016
    洛谷1875
    [模板] 强连通分量
  • 原文地址:https://www.cnblogs.com/itzxy/p/7544124.html
Copyright © 2011-2022 走看看