Splitting a multi-line environment variable into lines

后端 未结 3 2156
耶瑟儿~
耶瑟儿~ 2021-01-23 12:09

I have the following problem: I execute a windows batch file on a Jenkins server and have to split a multi-line environment variable (set vía a Jenkins parameter) into single li

3条回答
  •  梦谈多话
    2021-01-23 12:32

    Well as reflex I propose you the following solution:

    If python is installed (if not ... up to you to install it .. it is usefull ;-) )

    in your batch line : Python MyScriptToExecuteEachLinesFromVar.py varname "myprog.exe -baz 0 -meow"

    With the script content:

    import sys,os  # libraries used : system and operating system
    
    if len(sys.argv)>0:                   # if arguments provided (at least the variable name)
      varname = sys.argv[1]               # the variable name is the second argument (0=script)
      prefix = " ".join(sys.argv[2:])     # the list of all others are the prefix.
    
      lines = os.environ[varname].split('\n')  # get the var and split lines 
    
      for f in lines : 
        print "execute:", prefix, f
        os.system(prefix+" "+f)                # execute each line with the given prefix.
    

    Hope it helps.

提交回复
热议问题